基于Java Swing实现的简单计算器程序

这个计算器程序使用了Java Swing图形用户界面工具包来创建GUI界面,通过继承JFrame类并实现ActionListener接口来响应按钮点击事件,同时使用布局管理器来控制组件的排列。在程序中定义了一个currentResult变量来保存当前计算结果,一个currentOperator变量来保存当前操作符,以及一个startNewNumber变量来记录是否开始新数字。在每次点击操作符按钮时调用calculate()方法来执行上次操作符对应的计算,并更新currentResult、currentOperator和startNewNumber变量的值,最后将结果显示在文本框中。

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

public class Calculator extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JTextField displayField;
    private boolean startNewNumber = true;
    private double currentResult = 0.0;
    private String currentOperator = "=";

    public Calculator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("计算器");
        setSize(300, 250);
        setLocationRelativeTo(null);
        setResizable(false);

        JPanel panel = new JPanel(new BorderLayout());

        displayField = new JTextField(10);
        displayField.setEditable(false);
        displayField.setHorizontalAlignment(JTextField.RIGHT);
        panel.add(displayField, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel(new GridLayout(4, 4));

        addButton(buttonPanel, "7");
        addButton(buttonPanel, "8");
        addButton(buttonPanel, "9");
        addButton(buttonPanel, "/");

        addButton(buttonPanel, "4");
        addButton(buttonPanel, "5");
        addButton(buttonPanel, "6");
        addButton(buttonPanel, "*");

        addButton(buttonPanel, "1");
        addButton(buttonPanel, "2");
        addButton(buttonPanel, "3");
        addButton(buttonPanel, "-");

        addButton(buttonPanel, "0");
        addButton(buttonPanel, ".");
        addButton(buttonPanel, "=");
        addButton(buttonPanel, "+");

        panel.add(buttonPanel, BorderLayout.CENTER);

        setContentPane(panel);
    }

    private void addButton(Container container, String text) {
        JButton button = new JButton(text);
        button.addActionListener(this);
        container.add(button);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("C")) {
            currentResult = 0.0;
            currentOperator = "=";
            startNewNumber = true;
            displayField.setText("");
        } else if (command.equals("+/-")) {
            double value = Double.parseDouble(displayField.getText());
            displayField.setText(Double.toString(-value));
        } else if (command.equals("%")) {
            double value = Double.parseDouble(displayField.getText());
            displayField.setText(Double.toString(value / 100.0));
        } else if (command.equals(".")) {
            if (startNewNumber) {
                displayField.setText("0.");
                startNewNumber = false;
            } else if (displayField.getText().indexOf(".") < 0) {
                displayField.setText(displayField.getText() + ".");
            }
        } else if (command.matches("[0-9]")) {
            if (startNewNumber) {
                displayField.setText(command);
                startNewNumber = false;
            } else {
                displayField.setText(displayField.getText() + command);
            }
        } else {
            if (!startNewNumber) {
                calculate(Double.parseDouble(displayField.getText()));
                currentOperator = command;
                startNewNumber = true;
            } else {
                currentOperator = command;
            }
        }
    }

    private void calculate(double value) {
        switch (currentOperator) {
            case "+":
                currentResult += value;
                break;
            case "-":
                currentResult -= value;
                break;
            case "*":
                currentResult *= value;
                break;
            case "/":
                currentResult /= value;
                break;
            case "=":
                currentResult = value;
                break;
        }
        displayField.setText(Double.toString(currentResult));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Calculator calculator = new Calculator();
            calculator.setVisible(true);
        });
    }
}

你可能感兴趣的:(java,jvm,servlet)