gui简易加法运算器

GUI简易计算器

在oop原则中,组合大于继承,优先使用组合。

//加法计算器
package com.lean.www;

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

public class TestCalc {
    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) {
        //获得加数和被加数
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //将这个值加法运算后,放到第三个框
        num3.setText(""+(n1+n2));
        //清楚前两个框
        num1.setText("");
        num2.setText("");
    }
}

优化,完全改造为面向对象写法

package com.lean.www;

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

public class TestCalc {
     
    public static void main(String[] args) {
     
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
     
	//属性
    TextField num1 = new TextField(10);
    TextField num2 = new TextField(10);
    TextField num3 = new TextField(20);
	//方法
    public Calculator(){
     
        
		Label label = new Label("+");
        Button button = new Button("=");
        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;
    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 com.lean.www;

import java.awt.*;
import java.awt.event.*;

public class TestCalc {
     
    public static void main(String[] args) {
     
        new Calculator();
    }
}
//计算器类
class Calculator extends Frame{
     

    TextField num1 = new TextField(10);
    TextField num2 = new TextField(10);
    TextField num3 = new TextField(20);

    public Calculator(){
     

        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();
        windowClose(this);
        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("");
        }
    }
    private static void  windowClose(Frame frame) {
     
        frame.addWindowListener(new WindowAdapter() {
     
            @Override
            public void windowClosing(WindowEvent e) {
     
                System.exit(0);
            }
        });
    }

}

你可能感兴趣的:(gui简易加法运算器)