输入框时间监听
简易计算器
package com.chen.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTest01 {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
//三个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//一个标签
Label label = new Label("+");
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculator(num1,num2,num3));
//布局
setLayout(new FlowLayout());
setVisible(true);
setBounds(200,200,600,100);
add(num1);
add(label);
add(num2);
add(button);
add(num3);
}
class MyCalculator implements ActionListener {
private TextField num1, num2, num3;
public MyCalculator(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 n2 = Integer.parseInt(num2.getText());
TextField field = new TextField();
System.out.println(field.getText());
//将这个值 加法运算后,放到第三个框
num3.setText("" + (n1 + n2));
//清除前两个框
num1.setText("");
num2.setText("");
}
}
}