forked from fdzcxy212206324/zynlwj
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.6 KiB
112 lines
3.6 KiB
package java6226.lesson05;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
public class Calculator extends JFrame {
|
|
private JTextField display;
|
|
private double result = 0;
|
|
private String operator = "";
|
|
private boolean calculating = true;
|
|
|
|
public Calculator() {
|
|
setTitle("Simple Calculator");
|
|
setSize(250, 350);
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
setLayout(new BorderLayout());
|
|
|
|
display = new JTextField("0");
|
|
display.setEditable(false);
|
|
display.setHorizontalAlignment(JTextField.RIGHT);
|
|
add(display, BorderLayout.NORTH);
|
|
|
|
JPanel buttonPanel = new JPanel();
|
|
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5)); // 4行4列的按钮布局
|
|
|
|
String[] buttonLabels = {
|
|
"7", "8", "9", "/",
|
|
"4", "5", "6", "*",
|
|
"1", "2", "3", "-",
|
|
"C", "0", "=", "+"
|
|
};
|
|
|
|
for (String label : buttonLabels) {
|
|
JButton button = new JButton(label);
|
|
button.setFont(new Font("Arial", Font.BOLD, 18));
|
|
button.setPreferredSize(new Dimension(50, 40));
|
|
button.addActionListener(new ButtonClickListener());
|
|
buttonPanel.add(button);
|
|
}
|
|
|
|
add(buttonPanel, BorderLayout.CENTER);
|
|
setVisible(true);
|
|
}
|
|
|
|
private class ButtonClickListener implements ActionListener {
|
|
public void actionPerformed(ActionEvent e) {
|
|
String command = e.getActionCommand();
|
|
if (calculating) {
|
|
if (command.equals("C")) {
|
|
display.setText("0");
|
|
operator = "";
|
|
result = 0;
|
|
calculating = true;
|
|
} else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) {
|
|
operator = command;
|
|
calculating = false;
|
|
display.setText("");
|
|
} else if (command.equals("=")) {
|
|
calculate(Double.parseDouble(display.getText()));
|
|
} else {
|
|
display.setText(display.getText() + command);
|
|
}
|
|
} else {
|
|
if (command.equals("C")) {
|
|
display.setText("0");
|
|
operator = "";
|
|
result = 0;
|
|
calculating = true;
|
|
} else if (command.equals("=")) {
|
|
calculate(Double.parseDouble(display.getText()));
|
|
} else {
|
|
display.setText(display.getText() + command);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void calculate(double num) {
|
|
switch (operator) {
|
|
case "+":
|
|
result += num;
|
|
break;
|
|
case "-":
|
|
result -= num;
|
|
break;
|
|
case "*":
|
|
result *= num;
|
|
break;
|
|
case "/":
|
|
if (num != 0) {
|
|
result /= num;
|
|
} else {
|
|
display.setText("Error");
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
display.setText("" + result);
|
|
operator = "";
|
|
calculating = true;
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
public void run() {
|
|
new Calculator();
|
|
}
|
|
});
|
|
}
|
|
} |