JavaSwing制作简单计算器


源代码:

/**
 *
 * @author MMai
 * 2016/9/20
 *
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator {

    public static void main(String[] args) {
        MainJFrame go = new MainJFrame();
    }
  }

class MainJFrame {

    private final Font font = new Font("微软雅黑", Font.BOLD, 18);
    private boolean start;
    private double result;
    private String lastCommand;
    private JButton display;

   MainJFrame() {

        start = true;//计算器算法设置
        result = 0;
        lastCommand = "=";

        JFrame frame = new JFrame("计算器"); //框架

        ImageIcon icon = new ImageIcon(this.getClass().getResource("/img/icon.png"));//设置图标
        frame.setIconImage(icon.getImage());

      // JMenuBar menuBar = InitMenu(frame);
        frame.setJMenuBar(InitMenu(frame));// 设置菜单栏

       //JPanel mainPanel = InitMainPanel(frame);
        frame.add(InitMainPanel(frame), BorderLayout.CENTER);//设置计算面板

        frame.setSize(310, 260);  //框架属性设置
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

   //菜单栏
 private JMenuBar InitMenu(JFrame frame) {
        JMenuBar menuBar = new JMenuBar();
        menuBar.setFont(font);

        JMenu help = new JMenu("帮助(H)");
        help.setMnemonic('H');
        help.setFont(font);

        JMenuItem introduction = new JMenuItem("说明(T)");
        introduction.setFont(font);
        introduction.setMnemonic('T');

        JMenuItem about = new JMenuItem("关于(A)");
        about.setFont(font);
        about.setMnemonic('B');

        JMenuItem exit = new JMenuItem("退出(X)");
        exit.setFont(font);
        exit.setMnemonic('X');

        //
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        introduction.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                IntroductionDialog dialog = new IntroductionDialog(frame);
                dialog.setVisible(true);
            }
        });

        about.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AboutDialog dialog = new AboutDialog(frame);
                dialog.setVisible(true);
            }
        });

        help.add(introduction);
        help.add(about);
        help.addSeparator();
        help.add(exit);

        menuBar.add(help);
        return menuBar;
    }
 private JButton InitDisplay() {

        JButton input = new JButton("0");

        input.setFont(font);
        input.setHorizontalAlignment(JLabel.RIGHT);
        input.setEnabled(false);

        return input;
    }
 private JPanel InitMainPanel(JFrame frame) {

        JPanel panel = new JPanel(new GridLayout(4, 4));
        //new JPanel(new GridLayout(4, 4));

        display = InitDisplay();
        frame.add(display, BorderLayout.NORTH);

        ActionListener command = new CommandAction();
        ActionListener number = new NumberAction();

        panel.add(addButton("7", number));
        panel.add(addButton("8", number));
        panel.add(addButton("9", number));
        panel.add(addButton("*", command));

        panel.add(addButton("4", number));
        panel.add(addButton("5", number));
        panel.add(addButton("6", number));
        panel.add(addButton("-", command));

        panel.add(addButton("1", number));
        panel.add(addButton("2", number));
        panel.add(addButton("3", number));
        panel.add(addButton("+", command));

        panel.add(addButton("0", number));
        panel.add(addButton(".", command));
        panel.add(addButton("/", command));
        panel.add(addButton("=", command));

        return panel;

    }
 private JButton addButton(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.setFont(font);

        button.addActionListener(listener);

        button.setBackground(Color.WHITE);

        button.setOpaque(false);
        button.setFocusPainted(false);

        return button;
 }


 private class NumberAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
         String  input = e.getActionCommand();
            if (start) {
                display.setText(" ");
                start = false;
            }
            display.setText(display.getText() + input);
        }
}
private class CommandAction implements ActionListener  {

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (start) {
            if (command.equals("-")) {
                display.setText("-");
                start = false;
            } else {

                lastCommand = command;
            }
        } else {
            calculate(Double.parseDouble(display.getText()));
            lastCommand = command;
            start = true;
        }
    }
  }


private void calculate(double a) {
        switch (lastCommand) {
            case "+":
                result += a;
                break;
            case "-":
                result -= a;
                break;
            case "*":
                result *= a;
                break;
            case "/":
                result /= a;
                break;
            case "=":
                result = a;
                break;
            default:
                break;
        }

        display.setText(" " + result);


}
}

class AboutDialog extends JDialog {
public AboutDialog(JFrame mainFrame) {
        super(mainFrame, "关于", true); //

        JTextArea info = new JTextArea("          \t 计算器          \nAuthor:Lenmonreds SCAU\nTime:2016年9月20日\nQQ:792277840\nhttp://blog.csdn.net/lenmonreds");
        info.setFont(new Font("等线", Font.BOLD, 20));
        info.setLineWrap(true);
        info.setEditable(false);

        JButton off = new JButton("确定");

        off.setBackground(Color.WHITE);
        off.setOpaque(false);

        off.setFocusPainted(false);

        off.setFont(new Font("黑体", Font.BOLD, 20));
        off.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        JPanel panel = new JPanel();
        panel.add(off);

        add(panel, BorderLayout.SOUTH);
        setSize(400, 200);
        add(info, BorderLayout.CENTER);
        setLocationRelativeTo(mainFrame);
    }
}

class IntroductionDialog extends JDialog {
public IntroductionDialog(JFrame mainFrame) {
        super(mainFrame, "说明", true); //

        JTextArea info = new JTextArea("\t实现简单的计算器\n"
                + "几点说明: \n\t1目前仅支持鼠标输入\n\t2菜单栏快捷键Alt+\"Key\" \n\t3菜鸟编写,多有不足你忍着吧"
                + "\n可改进的地方:\n\t1扩展计算器的功能\n\t2增加快捷键,用键盘输入计算\n\t3界面可以更人性化,代码更优化\n\t4忘记设置置零了,只能用等号清 \n\t5:其他的一点什么,想到再说吧...");


        info.setFont(new Font("等线", Font.BOLD, 18));
        info.setLineWrap(true);
        info.setEditable(false);

        JPanel panel = new JPanel();
        add(panel, BorderLayout.SOUTH);
        setSize(400, 300);
        add(info, BorderLayout.CENTER);
        setLocationRelativeTo(mainFrame);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值