1.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//基础计算机
public class TestCalc {
public static void main(String[] args) {
new calculator();
}
}
class calculator extends Frame{
public Label num1;
public calculator(){
//文本框 2输入 1输出
TextField textField1=new TextField(10);
TextField textField2=new TextField(10);
TextField textField3=new TextField(20);
//1button
Button button=new Button("=");
button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));
// 1 标签
Label label=new Label("+");
//布局
setLayout(new FlowLayout());
//有顺序哦!!!
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
class MyCalculatorListener implements ActionListener{
private TextField textField1,textField2,textField3;
public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3) {
this.textField1=textField1;
this.textField2=textField2;
this.textField3=textField3;
}
public MyCalculatorListener(Calcular calcular) {
}
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(textField1.getText());
int n2=Integer.parseInt(textField2.getText());
//加一个“” 可以不用转换类型
textField3.setText(""+(n1+n2));
//clean
textField1.setText("");
textField2.setText("");
}
}
2.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc2 {
public static void main(String[] args) {
new Calcular().loadFrame();
}
}
class Calcular extends Frame{
static TextField num1;
static TextField num2;
static TextField num3;
public void loadFrame() {
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(20);
//1button
Button button=new Button("=");
// 1 标签
Label label=new Label("+");
button.addActionListener(new MyCalculatorListener1(this));
//布局
setLayout(new FlowLayout());
//有顺序哦!!!
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
class MyCalculatorListener1 implements ActionListener {
calculator calculator=null;
public MyCalculatorListener1(Calcular calcular) {
this.calculator= calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(Calcular.num1.getText());
int n2=Integer.parseInt(Calcular.num2.getText());
//加一个“” 可以不用转换类型
Calcular.num3.setText(""+(n1+n2));
//clean
Calcular.num1.setText("");
Calcular.num2.setText("");
}
}
3.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc3 {
public static void main(String[] args) {
new com.qing.lesson02.Calcular().loadFrame();
}
}
class Calculator extends Frame {
static TextField num1;
static TextField num2;
static TextField num3;
public void loadFrame() {
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(20);
//1button
Button button=new Button("=");
// 1 标签
Label label=new Label("+");
button.addActionListener(new MyCalculatorListener2());
//布局
setLayout(new FlowLayout());
//有顺序哦!!!
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//匿名内部类
private class MyCalculatorListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(com.qing.lesson02.Calcular.num1.getText());
int n2 = Integer.parseInt(com.qing.lesson02.Calcular.num2.getText());
//加一个“” 可以不用转换类型
com.qing.lesson02.Calcular.num3.setText("" + (n1 + n2));
//clean
com.qing.lesson02.Calcular.num1.setText("");
com.qing.lesson02.Calcular.num2.setText("");
}
}
}