练习:简易计算器
一.初版(面向过程)
package lesson02;
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 TextCalculator {
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);
//一个按钮
Button button = new Button("=");
//给按钮加监听事件
button.addActionListener(new MyTextActionEven03(num1,num2,num3) );
//一个标签
Label label = new Label("+");
//布局
setLayout( new FlowLayout() );
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听类
class MyTextActionEven03 implements ActionListener {
private TextField num1,num2,num3;
public MyTextActionEven03(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int a1 = Integer.parseInt(num1.getText());
int a2 = Integer.parseInt(num1.getText());
//将两数相加后放在第三个框
num3.setText(""+(a1+a2));
//按下按钮后清空前两个框
num1.setText("");
num2.setText("");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/98cba52721979573373f5a84a9f4a7fe.png)
二.改良版(面向对象)
package lesson02;
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 TextCalculator {
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.addActionListener(new MyTextActionEven03(this) );
//一个标签
Label label = new Label("+");
//布局
setLayout( new FlowLayout() );
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听类
class MyTextActionEven03 implements ActionListener {
//获取计算器这个对象,在一个类里组合另一个类
Calculator calculator;
public MyTextActionEven03(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数\
int a1 = Integer.parseInt(calculator.num1.getText());
int a2 = Integer.parseInt(calculator.num2.getText());
//将两数相加后放在第三个框
calculator.num3.setText(""+(a1+a2));
//按下按钮后清空前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
三.内部类
package lesson02;
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 TextCalculator {
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.addActionListener(new MyTextActionEven03());
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private class MyTextActionEven03 implements ActionListener {
//获取计算器这个对象,在一个类里组合另一个类
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数\
int a1 = Integer.parseInt(num1.getText());
int a2 = Integer.parseInt(num2.getText());
//将两数相加后放在第三个框
num3.setText(""+(a1+a2));
//按下按钮后清空前两个框
num1.setText("");
num2.setText("");
}
}
}
//监听类