组合+内部类、简易计算器
oop原则:组合>继承
-
计算器一版(未使用组合+内部类)
package GUI.lee.test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class Calc { public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); calculator.pack(); } } //计算器类 class Calculator extends Frame { public Calculator() { //三个文本框(位置) TextField num1 = new TextField(10); TextField num2 = new TextField(10); TextField num3 = new TextField(15); //一个按钮 Button button = new Button("="); button.addActionListener(new MyCalculatorLisener(num1, num2, num3)); //一个标签 Label label = new Label("+"); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); } } //监听器类 class MyCalculatorLisener implements ActionListener { //获取计算器这个对象 Calculator calculator = null; public MyCalculatorLisener(Calculator calculator) { } //获取三个变量的方法 private TextField num1, num2, num3; public MyCalculatorLisener(TextField num1, TextField num2, TextField num3) { this.num1 = num1; this.num2 = num2; this.num3 = num3; } @Override public void actionPerformed(ActionEvent e) { //1.获得加数、被加数(转换成字符串后) int i = Integer.parseInt(num1.getText()); int j = Integer.parseInt(num2.getText()); num2.getText(); //2.,相加 num3.setText(""+(i+j)); //3.清除前两个框 num1.setText(""); num2.setText(""); } }
-
计算器二版(使用组合,面向对象)
package GUI.lee.test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class Calc1 { public static void main(String[] args) { new Calculator1().LoFrame(); } } //计算器类 class Calculator1 extends Frame { //属性 TextField num1, num2,num3; //方法 public void LoFrame(){ //三个文本框(位置) num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(15); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorLisener1(this)); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorLisener1 implements ActionListener { //获取计算器这个对象,在一个类中组合另一个类 Calculator1 calculator1 = null; //获取对象的构造器 public MyCalculatorLisener1(Calculator1 calculator1) { this.calculator1 = calculator1; } @Override public void actionPerformed(ActionEvent e) { int i = Integer.parseInt(calculator1.num1.getText()); int j = Integer.parseInt(calculator1.num2.getText()); calculator1.num3.setText(""+(i+j)); calculator1.num1.setText(""); calculator1.num2.setText(""); } }
-
计算机三版(内部类,最推荐的一个思路)
package GUI.lee.test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class Calc2 { public static void main(String[] args) { new Calculator1().LoFrame(); } } //计算器类 class Calculator2 extends Frame { //属性 TextField num1, num2,num3; //方法 public void LoFrame(){ //三个文本框(位置) num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(15); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorLisener2()); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } //监听器类 private class MyCalculatorLisener2 implements ActionListener { //获取计算器这个对象,在一个类中组合另一个类 @Override public void actionPerformed(ActionEvent e) { int i = Integer.parseInt(num1.getText()); int j = Integer.parseInt(num2.getText()); num3.setText(""+(i+j)); num1.setText(""); num2.setText(""); } } }