java swing 实现简单计算器

gitee仓库地址:Calculator: 简单计算器 (gitee.com)https://gitee.com/misaka10000/calculator

常量定义:

package com.wuzihao.calculator;

import java.awt.*;

public class Constant {
    public static final String NAME = "简单计算器";
    public static final int FRAME_W = 700;
    public static final int FRAME_H = 700;

    public static final int SCREEN_W = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int SCREEN_H = Toolkit.getDefaultToolkit().getScreenSize().height;

    public static final int FRAME_X = (SCREEN_W - FRAME_W) / 2;
    public static final int FRAME_Y = (SCREEN_H - FRAME_H) / 2;


}

 初始化程序:

public void init(){
    this.setTitle(Constant.NAME);
    this.setVisible(true);
    this.setSize(Constant.FRAME_W, Constant.FRAME_H);
    this.setLocation(Constant.FRAME_X, Constant.FRAME_Y);
    this.setLayout(new BorderLayout());
    this.setResizable(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

计算器上层组件:

public void Component_North(){
    this.textField.setPreferredSize(new Dimension(650,50));
    this.jButton.setText("清空");
    this.jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText("");
        }
    });
    jPanel_north.add(textField);
    jPanel_north.add(jButton);
    this.add(jPanel_north,BorderLayout.NORTH);
}

计算器主体组件:

public void Component_Center(){
    String str = "123+456-789*0.=/";
    String regex = "[\\+\\-*=/]";
    this.jPanel_Center.setLayout(new GridLayout(4,4));
    for (int i = 0; i < 16; i++) {
        String substring = str.substring(i, i + 1);
        JButton jButton = new JButton();
        jButton.setText(substring);
        if(substring.matches(regex)){
            jButton.setFont(new Font("粗体",Font.BOLD,16));
        }
        jButton.addActionListener(this);
        jPanel_Center.add(jButton);
    }
    this.add(jPanel_Center,BorderLayout.CENTER);
}

监听方法实现:

@Override
public void actionPerformed(ActionEvent e) {
    String click = e.getActionCommand();
    if(".0123456789".contains(click)){
        this.textField.setText(textField.getText() + click);
    }else if(click.matches("[\\+\\-*/]{1}")){
        operator = click;
        firstInput = this.textField.getText();//保存当前的值
        this.textField.setText("");
    }else if(click.equals("=")){
        Double a = Double.valueOf(firstInput);
        Double b = Double.valueOf(textField.getText());
        Double result = null;

        switch(operator){
            case "+":
                result = a + b;
                break;
            case "-":
                result = a - b;
                break;
            case "*":
                result = a * b;
                break;
            case "/":
                result = a / b;
                break;
        }

        this.textField.setText(result.toString());
    }
}

页面效果展示:

 

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值