简易单行计算器,深化学习组合和内部类

面向过程(未优化的代码)

package GUI.lesson4;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCal {
     
    public static void main(String[] args) {
     
         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 MyCalculatorListener(num1,num2,num3));
        //一个标签
        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());//因为getText()中需要输入字符串,所以使用此方法将它变成int值
        int n2 = Integer.parseInt(num2.getText());

        //2.将这个值 +运算后,放到第三个框
        num3.setText(""+(n1+n2));

        //3.清除前两个框
        num1.setText("");
        num2.setText("");

    }
}

面向对象的方法(第一次优化)

package GUI.lesson4;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCal {
     
    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 MyCalculatorListener(this));
        Label label = new Label("+");

        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }

}

//监听器类
class MyCalculatorListener implements ActionListener{
     
    //获取计算器这个对象,在一个类中组合另外一个类
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator) {
     
       this.calculator = calculator;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
     
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText(""+(n1+n2));
        calculator.num1.setText("");
        calculator.num2.setText("");


    }
}

完全改造为面向对象(再次优化)

内部类操作:更好的包装

package GUI.lesson4;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TestCal {
     
    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 MyCalculatorListener());
        Label label = new Label("+");

        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) {
     
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }

}


你可能感兴趣的:(java)