java简易计算器
初学java的GUI编程,只支持加法import java.awt.*;
import java.awt.event.*;
public class calculator {
public static void main(String[] args) {
new mycalc();
}
}
class mycalc extends Frame {
public mycalc(){
Button button1 = new Button("compute");
Button button2 = new Button("empty");
TextField textField1 = new TextField(10);
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(10);
Label label1 = new Label("+");
Label label2 = new Label("=");
this.setTitle("计算器");
this.setBackground(new Color(255, 255, 255));
this.setBounds(500,500,236,80);
this.setVisible(true);
this.setLayout(new FlowLayout());
this.add(textField1);
this.add(label1);
this.add(textField2);
this.add(label2);
this.add(textField3);
this.add(button1);
this.add(button2);
this.pack();
mywframe myframe = new mywframe();
this.addWindowListener(myframe);
mybuttonlistener mybuttonlistener = new mybuttonlistener(textField1,textField2,textField3);
button1.addActionListener(mybuttonlistener);
mybutton2listener mybutton2listener = new mybutton2listener(textField1,textField2,textField3);
button2.addActionListener(mybutton2listener);
}
}
class mybuttonlistener implements ActionListener {
private TextField num4;
private TextField num5;
private TextField num6;
public mybuttonlistener(TextField num1,TextField num2,TextField num3){
this.num4 = num1;
this.num5 = num2;
this.num6 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
int num7 = Integer.parseInt(num4.getText());
int num8 = Integer.parseInt(num5.getText());
num6.setText(""+(num7+num8));
}
}
class mybutton2listener implements ActionListener{
private TextField num4;
private TextField num5;
private TextField num6;
public mybutton2listener(TextField num1,TextField num2,TextField num3){
this.num4 = num1;
this.num5 = num2;
this.num6 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
num4.setText("");
num5.setText("");
num6.setText("");
}
}
class mywframe implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
} 据我所知,Python终端都可以直接做加减乘除{:1_918:} {:1_921:}强,一会试试 本帖最后由 AiniWang 于 2022-7-18 09:30 编辑
// GUI简易计算器实现
public class TestCalcDemo {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame {
TextField num1, num2, num3;
public void loadFrame() {
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
button.setActionCommand("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setTitle("简易计算器-加法");
setVisible(true);
setBounds(300, 200, 500, 80);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭监听事件触发");
System.exit(0);
}
});
}
// 监听器类
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!num1.getText().equals("")) {
int n1 = Integer.parseInt(num1.getText());
if (!num2.getText().equals("")) {
int n2 = Integer.parseInt(num2.getText());
num3.setText(String.valueOf(n1 + n2));
num1.setText("");
num2.setText("");
} else {
System.out.println("被加数获取到为空");
}
} else {
System.out.println("加数获取到为空");
}
}
}
} 有意思的计算器,是java编出来的计算器。 skywalker0123 发表于 2022-7-17 22:19
据我所知,Python终端都可以直接做加减乘除
java 终端也可以 skywalker0123 发表于 2022-7-17 22:19
据我所知,Python终端都可以直接做加减乘除
java 11 有个jshell 有点跟python终端差不多了 Todd 发表于 2022-7-18 12:07
java 11 有个jshell 有点跟python终端差不多了
确实啊! 有意思的计算器,是java编出来的计算器。7
java 11 有个jshell 有点跟python终端差不多了
确实啊!
页:
[1]