Java 暂停程序,直到单击JButton
我四处寻找,找不到一个明确的答案。我已经写了一个游戏,需要暂停,直到用户点击他们的决定按钮,然后继续执行。有没有标准的方法可以做到这一点 我看到过类似的问题,涉及使用“wait()”和“notify()”,但我不确定是否需要添加更多线程,特别是因为我没有执行复杂或耗时的代码 我应该澄清一下,这是一款电脑版的棋盘游戏,所以只不过是一个带有一些组件的框架而已。以下是我想做的,谢谢各位:Java 暂停程序,直到单击JButton,java,swing,jbutton,Java,Swing,Jbutton,我四处寻找,找不到一个明确的答案。我已经写了一个游戏,需要暂停,直到用户点击他们的决定按钮,然后继续执行。有没有标准的方法可以做到这一点 我看到过类似的问题,涉及使用“wait()”和“notify()”,但我不确定是否需要添加更多线程,特别是因为我没有执行复杂或耗时的代码 我应该澄清一下,这是一款电脑版的棋盘游戏,所以只不过是一个带有一些组件的框架而已。以下是我想做的,谢谢各位: public class TreasureHunterFrame extends javax.swing.JFra
public class TreasureHunterFrame extends javax.swing.JFrame
{
public TreasureHunterFrame()
{
initComponents();
startNewGame();
}
private void startNewGame()
{
...
// User asked to click button while this method is running
synchronized (this) // wait until Stay of Leave button is clicked
{
try
{
while (!userHasMadeDecision)
this.wait();
}
catch (InterruptedException ie)
{
}
}
....
}
private void userStayButtonActionPerformed(java.awt.event.ActionEvent evt)
{
userHasMadeDecision = true;
userLeaving = false;
synchronized (this)
{
notifyAll();
}
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TreasureHunterFrame().setVisible(true);
}
});
}
}
试着发布一两件你实际尝试过但没有达到你想要的效果的事情。这取决于你的游戏引擎如何工作。如果你有你的线程,你可以建立一个等待状态。在为您找到解决方案之前,我们需要更多信息。请看答案。要更快地获得更好的帮助,请发布。它可能需要一个模态
JDialog
。