gpt4 book ai didi

java - JOptionPane.showConfirmDialog 不停止当前线程

转载 作者:行者123 更新时间:2023-12-02 06:39:27 25 4
gpt4 key购买 nike

正如我的标题所述,每当我调用它时,我的 showConfirmDialog 都不会等待按下"is"或“否”按钮。

最初我遇到了一个错误,导致我获得空白的 JOptionPanes,所以现在我使用 invokeLater 方法。我对这个概念不太熟悉,所以我提前道歉。

public int firstGame()
{

SwingUtilities.invokeLater(new Runnable() {
public void run() {
yOrN = JOptionPane.showConfirmDialog(null,
"Are you ready to play?\n", "Play?", JOptionPane.YES_NO_OPTION);
}
});
return yOrN;
// will return 0 if yes and 1 if no.

}

在使用invokeLater之前,它工作正常(除了空白的JOptionPane)。这个方法是否运行另一个线程?为什么我的 showInputDialog 等待输入而不是这个?

最佳答案

SwingUtilities.invokeLater 就是这样做的,它将 Runnable 放入事件队列中以便稍后运行,这意味着一旦您调用它,您的 return 语句几乎在它之后立即执行,并且在将来的某个时间,将显示 JOptionPane

此类请求被放入事件队列并由事件调度线程处理

看看How to pass results from EDT back to a different thread?寻找可能的解决方案...

关于java - JOptionPane.showConfirmDialog 不停止当前线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19288269/

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