Java GUI计算器

在这里插入图片描述

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
    private final GridBagLayout gbl = new GridBagLayout();//布局管理器
    private final GridBagConstraints gbc = new GridBagConstraints();
    String input = "";
    private final JTextField result;

    public Calculator() {
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 1;
        //结果文本组件
        result = new JTextField();
        result.setFont(new Font("宋体", Font.PLAIN, 20));
        addComponent(this, result, 0, 0, 4, 1.0);//result
        //顶部组件集合
        JButton jButtonTemp;
        JPanel topRow = new JPanel();
        addComponent(this, topRow, 0, 1, 4, 1.0);
        jButtonTemp = new JButton("C");
        jButtonTemp.addActionListener(this);
        addComponent(topRow, jButtonTemp, 0, 0, 1, 1.0);//JButton C
        jButtonTemp = new JButton("%");
        jButtonTemp.addActionListener(this);
        addComponent(topRow, jButtonTemp, 1, 0, 1, 0.33);//JButton %
        jButtonTemp = new JButton("+");
        jButtonTemp.addActionListener(this);
        addComponent(topRow, jButtonTemp, 2, 0, 1, 1.0);//JButton +
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {
                jButtonTemp = new JButton(((2 - j) * 3 + i + 1) + "");
                jButtonTemp.addActionListener(this);
                addComponent(this, jButtonTemp, i, j + 2, 1, 1.0);
            }
        jButtonTemp = new JButton("-");
        jButtonTemp.addActionListener(this);
        addComponent(this, jButtonTemp, 3, 2, 1, 1.0);//JButton -
        jButtonTemp = new JButton("x");
        jButtonTemp.addActionListener(this);
        addComponent(this, jButtonTemp, 3, 3, 1, 1.0);//JButton x
        jButtonTemp = new JButton("/");
        jButtonTemp.addActionListener(this);
        addComponent(this, jButtonTemp, 3, 4, 1, 1.0);//JButton /
        //底部组件集合
        JPanel bottomRow = new JPanel();
        addComponent(this, bottomRow, 0, 5, 4, 1.0);
        jButtonTemp = new JButton("0");
        jButtonTemp.addActionListener(this);
        addComponent(bottomRow, jButtonTemp, 0, 0, 1, 1.0);//JButton 0
        jButtonTemp = new JButton(".");
        jButtonTemp.addActionListener(this);
        addComponent(bottomRow, jButtonTemp, 1, 0, 1, 0.3);//JButton .
        jButtonTemp = new JButton("=");
        jButtonTemp.addActionListener(this);
        addComponent(bottomRow, jButtonTemp, 2, 0, 1, 1.0);//JButton =
        //属性设定
        this.setTitle("加黎盾计算器");
        this.setSize(600, 300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //自定义组件添加布局方法
    private void addComponent(Container con, Component comp, int gx, int gy, int gridwidth, double weightx) {
        if (!(con.getLayout() instanceof GridBagLayout)) con.setLayout(gbl);
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gridwidth;
        gbc.weightx = weightx;
        con.add(comp, gbc);
    }

    //监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        int cnt = 0;
        String actionCommand = e.getActionCommand();
        switch (actionCommand) {
            case "+", "-", "x", "/", "%" -> input += " " + actionCommand + " ";
            case "C" -> {
                result.setText("");
                input = "";
            }
            case "=" -> {
                try {//当发现计算错误,即发现表达式不合法时,提示ERROR
                    input += " = " + calc(input);
                    result.setText(input);
                } catch (Exception exception) {
                    result.setText("ERROR");
                }
                input = "";
                cnt = 1;
            }
            default -> input += actionCommand;
        }
        if (cnt == 0)
            result.setText(input);
    }

    //计算方法
    private String calc(String input) {
        String[] str;
        str = input.split(" ");
        Stack<Double> s = new Stack<Double>();
        double m = Double.parseDouble(str[0]);
        s.push(m);
        for (int i = 1; i < str.length; i++) {
            if (i % 2 == 1) {
                if (str[i].compareTo("+") == 0) {
                    double help = Double.parseDouble(str[i + 1]);
                    s.push(help);
                }
                if (str[i].compareTo("-") == 0) {
                    double help = Double.parseDouble(str[i + 1]);
                    s.push(-help);
                }
                if (str[i].compareTo("x") == 0) {
                    double help = Double.parseDouble(str[i + 1]);
                    double ans = s.peek();//取栈顶
                    s.pop();//消栈
                    ans *= help;
                    s.push(ans);
                }
                if (str[i].compareTo("/") == 0) {
                    double help = Double.parseDouble(str[i + 1]);
                    double ans = s.peek();
                    s.pop();
                    ans /= help;
                    s.push(ans);
                }
                if (str[i].compareTo("%") == 0) {
                    double help = Double.parseDouble(str[i + 1]);
                    double ans = s.peek();
                    s.pop();
                    ans %= help;
                    s.push(ans);
                }
            }
        }
        double ans = 0;
        while (!s.isEmpty()) {
            ans += s.peek();
            s.pop();
        }
        return String.valueOf(ans);
    }

    //主方法
    public static void main(String[] args) {
        new Calculator();
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值