简易计算器

2022/8/4 6:22:57

本文主要是介绍简易计算器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

组合

面向过程

public class TestCalc {
    public static void main(String[] args) {
new Calculate();
    }
}
class  Calculate extends Frame{
    public  Calculate(){
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new CalculateListener(num1,num2,num3));
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add((num3));

        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}
class  CalculateListener implements  ActionListener{
    private  TextField num1,num2,num3;
    public  CalculateListener(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("");

    }
}

面向对象

public class TestCalc {
    public static void main(String[] args) {
Calculate a=new Calculate();
a.loadFrame();
    }
}

class  Calculate extends  Frame{
    TextField num1,num2,num3;
    public  void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new CalculateListener(this));
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add((num3));

        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}


class  CalculateListener implements  ActionListener{
    Calculate calculate;
    public  CalculateListener(Calculate calculate){
        this.calculate=calculate;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1=Integer.parseInt(calculate.num1.getText());
        int n2=Integer.parseInt(calculate.num2.getText());

calculate.num3.setText(""+(n1+n2));
calculate.num1.setText("");
calculate.num2.setText("");

    }
}

内部类

public class TestCalc {
    public static void main(String[] args) {
Calculate a=new Calculate();
a.loadFrame();
    }
}

class  Calculate extends  Frame{
    TextField num1,num2,num3;
    class  CalculateListener 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("");

        }
    }
    public  void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new CalculateListener());
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add((num3));

        pack();
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

 



这篇关于简易计算器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程