Java swing组件实现简易计算器

Java swing组件实现简易计算器

使用swing组件实现的简易计算器效果图如下
演示效果
以下是源代码

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

public class cacu extends JFrame implements ActionListener {
    private String name[] = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/"};
    String s = "";
    int flag = 0;
    double x;
    private JButton button[] = new JButton[name.length];
    JPanel J = new JPanel();
    JPanel j = new JPanel();
    JTextField tf = new JTextField(11);

    public cacu() {
        super("计算机器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cp = getContentPane();
        cp.add(J, BorderLayout.NORTH);
        cp.add(j, BorderLayout.SOUTH);
        setSize(400, 200);
        JLabel L = new JLabel("结果");
        J.add(L);
        J.add(tf);
        GridLayout but = new GridLayout(4, 4, 2, 6);
        j.setLayout(but);
        Color c = new Color(200, 200, 80);
        tf.setBackground(c);
        for (int i = 0; i < name.length; i++) {
            button[i] = new JButton(name[i]);
            j.add(button[i]);
        }
        for (int i = 0; i < name.length; i++) {
            button[i].addActionListener(this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        tf.setText(tf.getText() + e.getActionCommand());
        if (e.getSource() == button[3]) {
            x = Double.parseDouble(s);
            flag = 1;
            tf.setText("");
            s = "";
        } else if (e.getSource() == button[7]) {
            x = Double.parseDouble(s);
            flag = 2;
            tf.setText("");
            s = "";
        } else if (e.getSource() == button[11]) {
            x = Double.parseDouble(s);
            flag = 3;
            tf.setText("");
            s = "";
        } else if (e.getSource() == button[15]) {
            x = Double.parseDouble(s);
            flag = 4;
            tf.setText("");
            s = "";
        } else if (e.getSource() == button[14]) {
            switch (flag) {
                case 1: {
                    x = x + Double.parseDouble(s);
                    String s = String.valueOf(x);
                    tf.setText(s);
                    break;
                }
                case 2: {
                    x = x - Double.parseDouble(s);
                    String s = String.valueOf(x);
                    tf.setText(s);
                    break;
                }
                case 3: {
                    x = x * Double.parseDouble(s);
                    String s = String.valueOf(x);
                    tf.setText(s);
                    break;
                }
                case 4: {
                    if (Double.parseDouble(s) == 0) {
                        tf.setText("除数不能为0");
                        break;
                    }
                    x = x / Double.parseDouble(s);
                    String s = String.valueOf(x);
                    tf.setText(s);
                    break;
                }
            }
        } else {
            s = s + e.getActionCommand();
            tf.setText(s);
        }
    }

    public static void main(String[] args) {
        cacu c = new cacu();
        c.setVisible(true);
    }
}

源码链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博扬java张

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值