1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{
//声明组件
JTextField t1, t2, t3;
JButton add, sub, tim, div;
JButton save;
//属性,变量
double result=0;

Calculator(){
init();
this.setTitle("Calculator");
this.setLayout(new FlowLayout());
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
//新建组件
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(20);
add=new JButton("+");
sub=new JButton("-");
tim=new JButton("*");
div=new JButton("/");
save=new JButton("save");

//添加组件
add(t1);
add(t2);
add(t3);
add(add);
add(sub);
add(tim);
add(div);
add(save);

//注册监听
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
sub.addActionListener(this);
add.addActionListener(this);
tim.addActionListener(this);
div.addActionListener(this);
save.addActionListener(this);
//额外功能
t3.setEditable(false);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String first=t1.getText();
String second=t2.getText();
double n1 =Double.parseDouble(first);
double n2=Double.parseDouble(second);

if(e.getSource().equals(add)) {
result=n1+n2;
}
else if(e.getSource().equals(sub)) {
result=n1-n2;
}
else if(e.getSource().equals(tim)) {
result=n1*n2;
}
else if(e.getSource().equals(div)) {
result=n1/n2;
}
else if(e.getSource().equals(save)) {
try {
File myObj=new File("result.txt");
if(myObj.createNewFile())
System.out.println("Successfully create the file "+myObj.getName());
else
System.out.println("The file already exists");
}catch(IOException e1) {
System.out.println("ERROR");
e1.printStackTrace();
}
try {
FileWriter w=new FileWriter("result.txt");
w.write(result+"\n");
System.out.println("Sucessfully wrote to the file");
w.close();
}catch(IOException e1) {
System.out.println("ERROR");
e1.printStackTrace();
}
}

t3.setText(result+"\n");

}

public static void main(String args[]) {
Calculator c=new Calculator();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calc extends JFrame implements ActionListener{

JButton b1,b2,b3,b4;
JTextField t1,t2,t3;

Calc(){
init();
this.setSize(300, 300);
this.setTitle("计算");
this.setLayout(new FlowLayout());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void init() {
b1=new JButton("加");
b2=new JButton("减");
b3=new JButton("乘");
b4=new JButton("除");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(20);

add(b1);
add(b2);
add(b3);
add(b4);
add(t1);
add(t2);
add(t3);

t1.addActionListener(this);
t2.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);

t3.setEditable(false);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

double n1;
double n2;
double result=0;

if(e.getSource().equals(b1)) {
try {
n1=Double.parseDouble(t1.getText());
n2=Double.parseDouble(t2.getText());
result=n1+n2;
t3.setText(String.valueOf(result));
}catch(NumberFormatException ee) {
t3.setText("请输入数字");
}
}
else if(e.getSource().equals(b2)) {
try {
n1=Double.parseDouble(t1.getText());
n2=Double.parseDouble(t2.getText());
result=n1-n2;
t3.setText(String.valueOf(result));
}catch(NumberFormatException ee) {
t3.setText("请输入数字");
}
}
else if(e.getSource().equals(b3)) {
try {
n1=Double.parseDouble(t1.getText());
n2=Double.parseDouble(t2.getText());
result=n1*n2;
t3.setText(String.valueOf(result));
}catch(NumberFormatException ee) {
t3.setText("请输入数字");
}
}
else if(e.getSource().equals(b4)) {
try {
n1=Double.parseDouble(t1.getText());
n2=Double.parseDouble(t2.getText());
result=n1*n2;
t3.setText(String.valueOf(result));
}catch(NumberFormatException ee) {
t3.setText("请输入数字");
}
}

}

public static void main(String args[]) {
Calc c=new Calc();
}
}