parent
8d2d2135ba
commit
d6043acd1e
@ -0,0 +1,97 @@
|
||||
package ProjectStudy;
|
||||
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class SimpleCalculator extends JFrame implements ActionListener {
|
||||
private JTextField display;
|
||||
private double num1 = 0.0;
|
||||
private double num2 = 0.0;
|
||||
private char operator = ' ';
|
||||
private boolean start = true;
|
||||
|
||||
public SimpleCalculator() {
|
||||
this.setTitle("简单计算器");
|
||||
this.setSize(300, 400);
|
||||
this.setDefaultCloseOperation(3);
|
||||
this.setLayout(new BorderLayout());
|
||||
this.display = new JTextField();
|
||||
this.display.setHorizontalAlignment(4);
|
||||
this.add(this.display, "North");
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.setLayout(new GridLayout(4, 4));
|
||||
String[] buttonLabels = new String[]{"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
|
||||
String[] var3 = buttonLabels;
|
||||
int var4 = buttonLabels.length;
|
||||
|
||||
for (int var5 = 0; var5 < var4; ++var5) {
|
||||
String label = var3[var5];
|
||||
JButton button = new JButton(label);
|
||||
button.addActionListener(this);
|
||||
buttonPanel.add(button);
|
||||
}
|
||||
|
||||
this.add(buttonPanel, "Center");
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String command = e.getActionCommand();
|
||||
if (!command.matches("[0-9]") && !command.equals(".")) {
|
||||
if (command.matches("[+\\-*/]")) {
|
||||
this.num1 = Double.parseDouble(this.display.getText());
|
||||
this.operator = command.charAt(0);
|
||||
this.start = true;
|
||||
} else if (command.equals("=")) {
|
||||
this.num2 = Double.parseDouble(this.display.getText());
|
||||
double result = this.calculate(this.num1, this.num2, this.operator);
|
||||
this.display.setText(String.valueOf(result));
|
||||
this.start = true;
|
||||
}
|
||||
} else if (this.start) {
|
||||
this.display.setText(command);
|
||||
this.start = false;
|
||||
} else {
|
||||
this.display.setText(this.display.getText() + command);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private double calculate(double num1, double num2, char operator) {
|
||||
switch (operator) {
|
||||
case '*':
|
||||
return num1 * num2;
|
||||
case '+':
|
||||
return num1 + num2;
|
||||
case ',':
|
||||
case '.':
|
||||
default:
|
||||
JOptionPane.showMessageDialog(this, "错误:无效的操作符!");
|
||||
return 0.0;
|
||||
case '-':
|
||||
return num1 - num2;
|
||||
case '/':
|
||||
if (num2 != 0.0) {
|
||||
return num1 / num2;
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "错误:除数不能为零!");
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
SimpleCalculator calculator = new SimpleCalculator();
|
||||
calculator.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue