用java编写一个简单的计算器(用于数字1、2、3、4的计算)

其中+、-、*、/都是button按钮右上角右缩小、最大化、关闭按钮。关闭按钮能用

同济装潢设计 2021-09-18 16:36 435 次浏览 赞 115

最新问答

  • 足疗沙发厂家

    public class Counter extends JFrame implements ActionListener{

    /**
    * Comment for serialVersionUID
    */
    private static final long serialVersionUID = -1379168350413982454L;

    TextField num1;
    TextField num2;
    TextField result;
    JButton clear;
    JButton plus;
    JTextArea j1;
    JTextArea j2;
    JButton aa;
    public Counter(){

    num1 = new TextField(5);//定义加数1
    num2 = new TextField(5);//定义加数2
    result = new TextField(5);//定义结果
    plus=new JButton("加");//定义加法按钮
    clear = new JButton("清空");//定义清空按钮
    j1 = new JTextArea("+");//定义符号 +
    j2 = new JTextArea("=");//定义符号 =
    j1.setEditable(false);//定义属性只读
    j2.setEditable(false);//定义加数1
    result.setEditable(false);//定义加数1
    aa = new JButton("隐藏");
    this.add(num1);
    this.add(j1);
    this.add(num2);
    this.add(j2);
    this.add(result);
    this.add(plus);
    this.add(clear);
    this.add(aa);

    this.setLayout(new FlowLayout());
    this.setBackground(Color.white);
    this.setVisible(true);
    this.setSize(500,200);
    this.setTitle("计算器");
    this.setLocationRelativeTo(null);//窗口初始位置在中间
    plus.addActionListener(this);
    clear.addActionListener(this);
    aa.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }

    public void actionPerformed(ActionEvent e){

    if (e.getSource()==clear){//点击清空按钮
    num1.setText("");
    num2.setText("");
    result.setText("");
    }else if(e.getSource()==plus){//点击“加”按钮
    result.setText(Integer.parseInt(num1.getText())+Integer.parseInt(num2.getText())+"");
    }else if(e.getSource()==aa){
    aa.setVisible(false);
    }
    }

    public static void main(String[] args) {
    new Counter();
    }
    /**
    * 判断输入的是否是数字
    * @param str
    * @return
    */
    public static boolean isInt(String str){
    try {
    Integer.parseInt(str);
    } catch (Exception e) {
    return false;
    }
    return true;
    }
    }

    浏览 496赞 114时间 2022-12-02
  • 大锅饭饭饭饭

    给我你的邮箱 我发一份最全的给你 我的QQ号是357981751

    浏览 200赞 99时间 2022-10-11

用java编写一个简单的计算器(用于数字1、2、3、4的计算)

其中+、-、*、/都是button按钮右上角右缩小、最大化、关闭按钮。关闭按钮能用