calculate.java

来自「这是一个用java写的图形界面计算器」· Java 代码 · 共 224 行

JAVA
224
字号
/*
 * Calculator.java
 *
 * Created on 2008年4月15日, 下午9:45
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author Leo
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScienceCalculator {

    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    public static void main(String[] args) {
        CalculatorFrame frame = new CalculatorFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class CalculatorFrame extends JFrame {

    JMenuBar menubar = new JMenuBar();
    JMenu operator_1 = new JMenu("编辑");
    JMenu operator_2 = new JMenu("查看");
    JMenu operator_3 = new JMenu("帮助");
    JMenuItem copy = new JMenuItem("复制");
    JMenuItem paste = new JMenuItem("粘贴");
    JTextField display = new JTextField(20);
    JPanel panel_1 = new JPanel();
    JPanel panel_2 = new JPanel();
    JPanel pResult = new JPanel();

    public CalculatorFrame() {
        this.setTitle("计算器");
        this.setSize(300, 200);    //设置大小 
        this.setResizable(false);  //固定大小
        this.setLocation(250, 100);
        //this.setBackground(Color.RED);
        this.setJMenuBar(menubar);
        menubar.add(operator_1);
        menubar.add(operator_2);
        menubar.add(operator_3);
        operator_1.add(copy);
        operator_1.add(paste);
        pResult.setLayout(new FlowLayout());
        pResult.add(display);
        // pack();     //自动调整大小 
        result = 0;             //初始化
        lastCommand = "=";
        start = true;
        display.setText("0");
        display.setEditable(false);
        display.setHorizontalAlignment(JTextField.RIGHT);
        ActionListener insert = new InsertAction();
        ActionListener command = new CommandAction();
        ActionListener Single = new SingleAction();

        panel_1.setLayout(new GridLayout(4, 3));
        panel_2.setLayout(new GridLayout(4, 2));

        addButton_1("7", insert);
        addButton_1("8", insert);
        addButton_1("9", insert);
        addButton_2("/", command);
        addButton_2("sqrt", Single);

        addButton_1("4", insert);
        addButton_1("5", insert);
        addButton_1("6", insert);
        addButton_2("*", command);
        addButton_2("%", Single);

        addButton_1("1", insert);
        addButton_1("2", insert);
        addButton_1("3", insert);
        addButton_2("-", command);
        addButton_2("1/x", Single);

        addButton_1("0", insert);
        addButton_1("C", insert);
        addButton_1(".", insert);
        addButton_2("+", command);
        addButton_2("=", command);


        add(panel_1, BorderLayout.CENTER);
        add(panel_2, BorderLayout.EAST);
        Container ct = getContentPane();
        ct.setLayout(new BorderLayout(4, 5));
        ct.add(pResult, BorderLayout.NORTH);
        ct.add(panel_1, BorderLayout.CENTER);
        ct.add(panel_2, BorderLayout.EAST);
    }

    private void addButton_1(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        if (label.equals("C")) {
            button.setForeground(Color.RED);
        }
        panel_1.add(button);
    }

    private void addButton_2(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        if (label.equals("C") || label.equals("+") || label.equals("-") || label.equals("*") || label.equals("/") || label.equals("=")) {
            button.setForeground(Color.RED);
        }
        panel_2.add(button);
    }

    private class InsertAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String input = event.getActionCommand();
            if (input.equals("C")) //重新输入
            {
                display.setText(" ");
                result = 0;             //初始化
                lastCommand = "=";
                start = true;
                return;
            } else if (start) {
                if (input.equals(".")) {
                    display.setText("0");
                    start = false;
                } else {
                    display.setText("");
                    start = false;
                } 
            }
            if (input.equals(".")) {
                if (count == 1) {
                    input = "";
                }
                count = 1;
            }
            display.setText(display.getText() + input);
        }
    }

    private class CommandAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            count = 0;
            if (start) {
                if (command.equals("-")) {     //如果第一次输入的是“-”说明是一个负数
                    display.setText(command);
                    start = false;
                } else {
                    lastCommand = command;
                }
            } else {
                calculator(Double.parseDouble(display.getText()));
                lastCommand = command;     //保存运算符
                start = true;
            }
        }
    }

    private class SingleAction implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            count = 0;
            String command = event.getActionCommand();
            if (command.equals("sqrt") || command.equals("%") || command.equals("1/x")) {
                lastCommand = command;
                calculator_Single(Double.parseDouble(display.getText()));
            }
        }
    }

    public void calculator(double x) {
        if (lastCommand.equals("+")) {
            result += x;
        } else if (lastCommand.equals("-")) {
            result -= x;
        } else if (lastCommand.equals("*")) {
            result *= x;
        } else if (lastCommand.equals("/")) {
            result /= x;
        } else if (lastCommand.equals("=")) {
            result = x;
        }
        display.setText("" + result);

    }

    //    -------------------单目运算------------------------------
    public void calculator_Single(double x) {
        if (lastCommand.equals("sqrt")) {
            result = x;
            if (x < 0) {
                result = 0;
            //return;
            } else {
                result = Math.sqrt(x);
            }
        } else if (lastCommand.equals("%")) {
            result = x / 100;
        } else if (lastCommand.equals("1/x")) {
            result = 1 / x;
        }
        display.setText(" " + result);
    }
    //private JTextField display;
    private int count = 0;
    private boolean start;
    private double result;
    private String lastCommand;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?