简易计算器


组合

简易计算器

面向过程

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);
            }
        });
    }
}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/278803.html

(0)
上一篇 2022年8月4日
下一篇 2022年8月4日

相关推荐

发表回复

登录后才能评论