如何添加等待jButton1才能点击继续?
我有这段代码:
print("Guess the hidden number between 1 and 100")
guess = int(input("Enter your guess:\n")
if guess==67:
print("Correct Well Done")
elif guess<67:
print("Your guess is too low. Try again.")
else guess>67:
print("Your guess is too high. Try again.")
我已经做到了这一点,但它不起作用:
while (gameStatus == Status.CONTINUE){
////here add wait for jButton1 click
sumOfDice = rollDice(); // roll dice again
}
答案 0 :(得分:2)
您将线性命令行编程与事件驱动的GUI编程混淆,这是行不通的。您不必像现在一样等待,而是需要在事件发生时对事件做出响应,以及您的响应方式取决于程序的状态(实际上是模型的状态)。
请注意,这不会编译:
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e, int sumOfDice) {
sumOfDice = rollDice();
}
});
因为actionPerformed是一个重写方法,只能接受一个参数
更好的是:
jButton1.addActionListener(new ActionListener() {
private int sumOfDice = 0;
@Override // don't forget this important annotation
public void actionPerformed(ActionEvent e) {
if (gameStatus == Status.CONTINUE) {
sumOfDice = rollDice();
}
}
});
有关更详细和具体的答案,请考虑发布有关您的问题的更多详细信息以及相关代码。