计算器原始代码(面向过程)

计算器原始代码(面向过程)

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
package com.dwx.Prame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class testCale {
public static void main(String[] args) {
new Cale();
}
}
class Cale extends Frame{
public Cale(){
//在窗口上 我们需要三个文本框 一个标签 一个按钮
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(11);

Label label = new Label("+");

Button button = new Button("=");
button.addActionListener(new MyCale(num1,num2,num3)); //将三个文本框作为参数传递给下边的方法
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});

}

}
class MyCale implements ActionListener{

//获取三个变量
//将第三个变量的值设为前两个变量的和
//将前两个变量清空
private TextField num1,num2,num3; //设置三个本地的参数来承接传递过来的参数
public MyCale(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;

}
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());//将字符串类型强制转换成int类型的方法
int n2 = Integer.parseInt(num2.getText());

num3.setText(""+(n1+n2));

num1.setText("");
num2.setText("");

}
}

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信