组合+内部类-简易计算器的编程代码-2022-12-8
改进前代码:
package com.lr.guifirstdemo;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testTextDemo09 {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
//计算器类
class Calculator extends Frame {
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
button.addActionListener(new myCalculatorlistener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class myCalculatorlistener implements ActionListener{
private TextField num1,num2,num3;
public myCalculatorlistener(TextField num1, TextField num2, TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1、获取加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2、将计算后和的值放入第三个框
num3.setText(""+(n1+n2));
//3、清空第1,2个框
num1.setText("");
num2.setText("");
}
}
改进为面向对象后:
package com.lr.guifirstdemo;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testTextDemo09 {
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("=");
Label label = new Label("+");
button.addActionListener(new myCalculatorlistener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class myCalculatorlistener implements ActionListener{
// 获取计算器这个对象,在一个类中组合另一个类
Calculator calculator = null ;
//private TextField num1,num2,num3;
public myCalculatorlistener(Calculator calculator){
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1、获取加数和被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2、将计算后和的值放入第三个框
calculator.num3.setText(""+(n1+n2));
//3、清空第1,2个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:
package com.lr.guifirstdemo;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testTextDemo09 {
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("=");
Label label = new Label("+");
button.addActionListener(new myCalculatorlistener());
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
private class myCalculatorlistener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1、获取加数和被加数
//2、将计算后和的值放入第三个框
//3、清空第1,2个框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}