Java Swing 实现简易计算器

在这里插入图片描述

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Calcuator {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            CalcuatorFrame frame = new CalcuatorFrame();
            frame.setSize(240, 270);
            frame.setLocation(300, 200);
            frame.setTitle("Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class CalcuatorFrame extends JFrame {
    private JPanel panel;
    public JButton result;
    private JButton button0 = new JButton("0");
    private JButton button1 = new JButton("1");
    private JButton button2 = new JButton("2");
    private JButton button3 = new JButton("3");
    private JButton button4 = new JButton("4");
    private JButton button5 = new JButton("5");
    private JButton button6 = new JButton("6");
    private JButton button7 = new JButton("7");
    private JButton button8 = new JButton("8");
    private JButton button9 = new JButton("9");
    private JButton button = new JButton("");
    private JButton buttonplus = new JButton("+");
    private JButton buttonminus = new JButton("-");
    private JButton buttonmutliple = new JButton("*");
    private JButton buttondivise = new JButton("/");
    private JButton buttonpoint = new JButton(".");
    private JButton buttonequal = new JButton("=");
    int clear = 0;// qingling
    int c = 0;// x1 x2
    int operator = 0;// +-*/
    int point = 0;
    int f = 0;

    double x1 = 0;
    double x2 = 0;

    public CalcuatorFrame() {
        add(button);

        panel = new JPanel();

        panel.setLayout(new GridLayout(4, 4));

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(buttonplus);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(buttonminus);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(buttonmutliple);
        panel.add(button0);
        panel.add(buttonpoint);
        panel.add(button0);
        panel.add(buttonpoint);
        panel.add(buttonequal);
        panel.add(buttondivise);

        add(panel, BorderLayout.SOUTH);

        button0.addActionListener(new NumberAction(0));
        button1.addActionListener(new NumberAction(1));
        button2.addActionListener(new NumberAction(2));
        button3.addActionListener(new NumberAction(3));
        button4.addActionListener(new NumberAction(4));
        button5.addActionListener(new NumberAction(5));
        button6.addActionListener(new NumberAction(6));
        button7.addActionListener(new NumberAction(7));
        button8.addActionListener(new NumberAction(8));
        button9.addActionListener(new NumberAction(9));

        buttonplus.addActionListener(new OperatorAction(1));
        buttonminus.addActionListener(new OperatorAction(2));
        buttonmutliple.addActionListener(new OperatorAction(3));
        buttondivise.addActionListener(new OperatorAction(4));
        buttonequal.addActionListener(event -> {

            if (x2 != 0) {
                if (operator == 1)
                    x1 += x2;
                if (operator == 2)
                    x1 -= x2;
                if (operator == 3)
                    x1 *= x2;
                if (operator == 4)
                    x1 /= x2;
            }
            button.setText("" + x1);
            clear = 1;
            c = 0;
            x2 = 0;
            point = 0;
            f = 1;
        });

        buttonpoint.addActionListener(event -> {

            point = 1;
            button.setText(button.getText() + ".");
        });

        button.addActionListener(event -> {
            clear = 0;
            c = 0;
            operator = 0;
            point = 0;
            x1 = 0;
            x2 = 0;
            button.setText("");
        });
    }

    class NumberAction extends AbstractAction {
        public NumberAction(int x) {
            putValue(Action.NAME, "" + x);
            putValue("x", x);
        }

        public void actionPerformed(ActionEvent event) {
            Integer X = (Integer) getValue("x");
            int x = X.intValue();
            if (f == 1) {
                x1 = 0;
                c = 0;
            }
            if (clear == 1) {
                button.setText("");
                clear = 0;
            }
            if (c == 0) {
                if (point != 0) {
                    double w = x;
                    for (int i = 0; i < point; i++) {
                        w /= 10;
                    }
                    x1 += w;
                    point++;
                } else
                    x1 = x1 * 10 + x;
            }
            if (c == 1) {
                if (point != 0) {
                    double w = x;
                    for (int i = 0; i < point; i++) {
                        w /= 10;
                    }
                    x2 += w;
                    point++;
                } else
                    x2 = x2 * 10 + x;
            }
            f = 0;
            button.setText(button.getText() + x);
        }
    }

    class OperatorAction extends AbstractAction {
        int operator1;

        public OperatorAction(int operator1) {
            this.operator1 = operator1;
        }

        public void actionPerformed(ActionEvent event) {
            f = 0;
            if (operator1 == 1)
                button.setText("+");
            if (operator1 == 2)
                button.setText("-");
            if (operator1 == 3)
                button.setText("*");
            if (operator1 == 4)
                button.setText("/");
            c = 1;
            clear = 1;
            operator = operator1;
            point = 0;
        }
    }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值