修改计算器计算功能

main
2745873016 1 month ago
parent 043e8fd584
commit 56776f3aa8

@ -7,41 +7,6 @@ import java.awt.event.ActionEvent;
public class Calculator extends JFrame {
private JTextField inputField; // 输入显示区域
public Calculator() {
createUI();
}
private void createUI() {
// 设置窗口布局
setLayout(new BorderLayout());
// 创建输入显示区域
inputField = new JTextField("0");
inputField.setEditable(false);
inputField.setHorizontalAlignment(JTextField.RIGHT);
add(inputField, BorderLayout.NORTH);
// 创建面板用于放置按钮
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5)); // 设置间距
// 创建数字和运算符按钮
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+", "C", "=》"};
for (String b : buttons) {
JButton button = new JButton(b);
panel.add(button);
button.addActionListener(e -> press(e.getActionCommand()));
}
// 添加面板到窗口
add(panel, BorderLayout.CENTER);
// 设置窗口属性
setSize(500, 600); // 设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("计算器");
setVisible(true);
}
private void press(String command) {
if ("C".equals(command)) {

Loading…
Cancel
Save