千余千与

简易计算器

简易计算器

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 textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(15);
        //按钮
        Button button = new Button(" = ");
        button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));
        //标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);
    }
}
class  MyCalculatorListener implements ActionListener{
    //获取变量
    private  TextField textField1,textField2,textField3;
    public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取加数和被减数
        int n1 = Integer.parseInt(textField1.getText());
        int n2 = Integer.parseInt(textField1.getText());
        //将这各值加法运算后放到第三个框
        textField3.setText(""+(n1+n2));
        //清除前面两个框
        textField1.setText(" ");
        textField2.setText(" ");
    }
}

image-20210206161824339

写法二

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();

    }
}
class Calculator extends Frame{
    //属性
    TextField textField1,textField2,textField3;
    //方法
    public void loadFrame(){
        //文本框
        textField1 = new TextField(10);//字符数
        textField2 = new TextField(10);
        textField3 = new TextField(15);
        //按钮
        Button button = new Button(" = ");
        //标签
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener(this));
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        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.textField1.getText());
        int n2 = Integer.parseInt(calculator.textField2.getText());
        //将这各值加法运算后放到第三个框
        calculator.textField3.setText(""+(n1+n2));
        //清除前面两个框
        calculator.textField1.setText("");
        calculator.textField2.setText("");

    }
}

写法三

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();

    }
}
class Calculator extends Frame{
    //属性
    TextField textField1,textField2,textField3;
    //方法
    public void loadFrame(){
        //文本框
        textField1 = new TextField(10);//字符数
        textField2 = new TextField(10);
        textField3 = new TextField(15);
        //按钮
        Button button = new Button(" = ");
        //标签
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener());
        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        pack();
        setVisible(true);

}
private class  MyCalculatorListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(textField1.getText());
        int n2 = Integer.parseInt(textField2.getText());
        //将这各值加法运算后放到第三个框
        textField3.setText("" + (n1 + n2));
        //清除前面两个框
        textField1.setText("");
        textField2.setText("");

      }
    }
}

posted on 2021-02-06 16:20  千余千与  阅读(40)  评论(0编辑  收藏  举报

导航