JavaSwing制作简单计算器

来源:互联网 发布:企业工商数据api 编辑:程序博客网 时间:2024/05/01 19:56
源代码:/** * * @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 0
原创粉丝点击