parent
a800b9ed99
commit
8d06a64bec
@ -0,0 +1,106 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class Counter2158 extends JFrame implements ActionListener {
|
||||||
|
private double result;
|
||||||
|
private JTextField resultField;
|
||||||
|
|
||||||
|
public Counter2158() {
|
||||||
|
super("Simple Calculator");
|
||||||
|
initializeCalculator();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeCalculator() {
|
||||||
|
this.result = 0;
|
||||||
|
|
||||||
|
// 创建布局
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// 创建结果显示区域
|
||||||
|
resultField = new JTextField("0", 10);
|
||||||
|
resultField.setEditable(false);
|
||||||
|
add(resultField, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
// 创建按钮面板
|
||||||
|
JPanel buttonPanel = new JPanel();
|
||||||
|
buttonPanel.setLayout(new GridLayout(4, 4));
|
||||||
|
|
||||||
|
// 添加按钮
|
||||||
|
String[] buttons = {"+", "-", "*", "/", "Reset", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
||||||
|
for (String buttonText : buttons) {
|
||||||
|
JButton button = new JButton(buttonText);
|
||||||
|
button.addActionListener(this);
|
||||||
|
buttonPanel.add(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
add(buttonPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// 设置窗口属性
|
||||||
|
setSize(300, 200);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true); // 显示窗口
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
String command = e.getActionCommand();
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case "+":
|
||||||
|
add(Double.parseDouble(resultField.getText()));
|
||||||
|
break;
|
||||||
|
case "-":
|
||||||
|
subtract(Double.parseDouble(resultField.getText()));
|
||||||
|
break;
|
||||||
|
case "*":
|
||||||
|
multiply(Double.parseDouble(resultField.getText()));
|
||||||
|
break;
|
||||||
|
case "/":
|
||||||
|
try {
|
||||||
|
divide(Double.parseDouble(resultField.getText()));
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "Reset":
|
||||||
|
reset();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
add(Double.parseDouble(command));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
resultField.setText(String.valueOf(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(double num) {
|
||||||
|
result += num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void subtract(double num) {
|
||||||
|
result -= num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void multiply(double num) {
|
||||||
|
result *= num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void divide(double num) {
|
||||||
|
if (num == 0) {
|
||||||
|
throw new IllegalArgumentException("Divisor cannot be zero.");
|
||||||
|
}
|
||||||
|
result /= num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(Counter2158::new);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
||||||
|
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
||||||
|
// 查看 IntelliJ IDEA 建议如何修正。
|
||||||
|
System.out.print("Hello and welcome!");
|
||||||
|
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
||||||
|
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
||||||
|
System.out.println("i = " + i);
|
||||||
|
}}}
|
Loading…
Reference in new issue