gpt4 book ai didi

java - 我正在制作一个简单的计算器,但 GUI 构建一直卡住

转载 作者:行者123 更新时间:2023-11-30 06:18:46 24 4
gpt4 key购买 nike

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JLabel;

public class Calculator extends JFrame implements ActionListener {

private JPanel contentPane;
JButton button, btnNewButton, button_1, button_2, button_3, button_4,
button_5, button_6, button_7, button_8, btnClear, btnNewButton_1,
button_9, btnX, button_11;
JLabel lblNewLabel;
int v1 = 0;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Calculator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 415);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

btnNewButton = new JButton("7");
btnNewButton.setBounds(10, 130, 52, 37);
contentPane.add(btnNewButton);
btnNewButton.setActionCommand("7");
btnNewButton.addActionListener(this);

button = new JButton("8");
button.setBounds(72, 130, 52, 37);
contentPane.add(button);
button.setActionCommand("8");
button.addActionListener(this);

button_1 = new JButton("9");
button_1.setBounds(134, 130, 52, 37);
contentPane.add(button_1);
button_1.setActionCommand("9");
button_1.addActionListener(this);

button_2 = new JButton("6");
button_2.setBounds(134, 178, 52, 37);
contentPane.add(button_2);
button_2.setActionCommand("6");
button_2.addActionListener(this);

button_3 = new JButton("5");
button_3.setBounds(72, 178, 52, 37);
contentPane.add(button_3);
button_3.setActionCommand("5");
button_3.addActionListener(this);

button_4 = new JButton("4");
button_4.setBounds(10, 178, 52, 37);
contentPane.add(button_4);
button_4.setActionCommand("4");
button_4.addActionListener(this);

button_5 = new JButton("3");
button_5.setBounds(134, 226, 52, 37);
contentPane.add(button_5);
button_5.setActionCommand("3");
button_5.addActionListener(this);

button_6 = new JButton("2");
button_6.setBounds(72, 226, 52, 37);
contentPane.add(button_6);
button_6.setActionCommand("2");
button_6.addActionListener(this);

button_7 = new JButton("1");
button_7.setBounds(10, 226, 52, 37);
contentPane.add(button_7);
button_7.setActionCommand("1");
button_7.addActionListener(this);

button_8 = new JButton("0");
button_8.setBounds(72, 274, 52, 37);
contentPane.add(button_8);
button_8.setActionCommand("0");
button_8.addActionListener(this);

btnClear = new JButton("Clear ");
btnClear.setBounds(53, 343, 89, 23);
contentPane.add(btnClear);
btnClear.setActionCommand("Clear");
btnClear.addActionListener(this);

btnNewButton_1 = new JButton("+");
btnNewButton_1.setBackground(new Color(255, 255, 224));
btnNewButton_1.setForeground(Color.DARK_GRAY);
btnNewButton_1.setBounds(193, 130, 41, 36);
contentPane.add(btnNewButton_1);
btnNewButton_1.setActionCommand("+");
btnNewButton_1.addActionListener(this);

button_9 = new JButton("-");
button_9.setBackground(new Color(255, 255, 224));
button_9.setForeground(Color.DARK_GRAY);
button_9.setBounds(193, 179, 41, 36);
contentPane.add(button_9);
button_9.setActionCommand("-");
button_9.addActionListener(this);

btnX = new JButton("x");
btnX.setBackground(new Color(255, 255, 224));
btnX.setForeground(Color.DARK_GRAY);
btnX.setBounds(193, 226, 41, 36);
contentPane.add(btnX);
btnX.setActionCommand("x");
btnX.addActionListener(this);

button_11 = new JButton("\u00F7");
button_11.setBackground(new Color(255, 255, 224));
button_11.setForeground(Color.DARK_GRAY);
button_11.setBounds(193, 274, 41, 36);
contentPane.add(button_11);
button_11.setActionCommand("/");
button_11.addActionListener(this);

lblNewLabel = new JLabel("");
lblNewLabel.setBackground(new Color(255, 255, 255));
lblNewLabel.setBounds(10, 21, 214, 37);
contentPane.add(lblNewLabel);
}

@Override
public void actionPerformed(ActionEvent e) {
String eventName = e.getActionCommand();
if (eventName.equals("1")) {
lblNewLabel.setText("1");
v1 = 1;
} else if (eventName.equals("2")) {
lblNewLabel.setText("2");
v1 = 2;
} else if (eventName.equals("3")) {
lblNewLabel.setText("3");
v1 = 3;
} else if (eventName.equals("4")) {
lblNewLabel.setText("4");
v1 = 4;
} else if (eventName.equals("5")) {
lblNewLabel.setText("5");
v1 = 5;
} else if (eventName.equals("6")) {
lblNewLabel.setText("6");
v1 = 6;
} else if (eventName.equals("7")) {
lblNewLabel.setText("7");
v1 = 7;
} else if (eventName.equals("8")) {
lblNewLabel.setText("8");
v1 = 8;
} else if (eventName.equals("9")) {
lblNewLabel.setText("9");
v1 = 9;
} else if (eventName.equals("0")) {
lblNewLabel.setText("0");
v1 = 0;
} else if (eventName.equals("Clear")) {
lblNewLabel.setText("");
}
while (!eventName.equals("+") || !eventName.equals("-")
|| !eventName.equals("x") || !eventName.equals("/")) {
if (eventName.equals("1")) {
v1 = (v1 * 10) + 1;
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("2")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("3")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("4")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("5")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("6")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("7")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("8")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("9")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("0")) {
lblNewLabel.setText(Integer.toString(v1));
} else if (eventName.equals("Clear")) {
lblNewLabel.setText("");
}

}

}// overall loop

}// end

每次我运行 GUI 时,按钮在单击后立即卡住。我希望你们能帮助我。我觉得问题好像出在我第二种方法的 while 循环中,但我们将不胜感激。

最佳答案

是的,这个循环将永远持续下去。使用 if 语句代替循环。 Swing 会自动为您循环(每次按下按钮时都会调用该方法)。

关于java - 我正在制作一个简单的计算器,但 GUI 构建一直卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com