当前代码
package Lister;
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 Calc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
TextField textField1=new TextField(10);
TextField textField2=new TextField(10);
TextField textField3=new TextField(10);
//1个按钮
Button button=new Button("=");
button.addActionListener(new MyCalculatorLiner(textField1,textField2,textField3));
//1个标签
Label label=new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
//监听器类
class MyCalculatorLiner implements ActionListener {
private TextField textField1,textField2,textField3;
public MyCalculatorLiner(TextField textField1,TextField textField2,TextField textField3,){
this.textField1=textField1;
this.textField2=textField2;
this.textField3=textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取两个需要计算的数
int n1=Integer.parseInt(textField1.getText());
int n2=Integer.parseInt(textField2.getText());
//计算两个数的和
textField3.setText(""+(n1+n2));
//清除文本框的文本
textField1.setText("");
textField2.setText("");
}
}
完全改造为面向对象
package Lister;
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 Calc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//3个文本框
textField1=new TextField(10);
textField2=new TextField(10);
textField3=new TextField(10);
//1个按钮
Button button=new Button("=");
button.addActionListener(new MyCalculatorLiner(this));
//1个标签
Label label=new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
//监听器类
class MyCalculatorLiner implements ActionListener {
//获取计算器这个对象,在一个类组合另一个类
Calculator calculator=null;
public MyCalculatorLiner(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(calculator.textField1.getText());
int n2=Integer.parseInt(calculator.textField2.getText());
calculator.textField3.setText(""+(n1+n2));
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
内部类
package Lister;
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 Calc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//3个文本框
textField1=new TextField(10);
textField2=new TextField(10);
textField3=new TextField(10);
//1个按钮
Button button=new Button("=");
button.addActionListener(new MyCalculatorLiner());
//1个标签
Label label=new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
//监听器类
//内部类最大的好处,就是可以畅通无阻的使用外部类的属性和方法!
private class MyCalculatorLiner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int n1=Integer.parseInt(textField1.getText());
int n2=Integer.parseInt(textField2.getText());
textField3.setText(""+(n1+n2));
textField1.setText("");
textField2.setText("");
}
}
}