显示“JOptionPane.showMessageDialog”不停止执行流程 [英] Showing "JOptionPane.showMessageDialog" without stopping flow of execution
问题描述
我目前正在开展一个比我原先想象的更复杂的项目。我现在的目标是显示一个消息对话框,而不会停止程序中主线程的执行。现在,我正在使用:
I'm currently working on a project that's getting more complex than I thought it would be originally. What I'm aiming to do right now is show a message dialog without halting the execution of the main thread in the program. Right now, I'm using:
JOptionPane.showMessageDialog(null, message, "Received Message", JOptionPane.INFORMATION_MESSAGE);
但这会暂停主线程中的其他所有内容,因此它不会一次显示多个对话框,就在另一个之后。这个m =可以像创建一个新的JFrame一样简单而不是使用JOptionPane吗?
But this pauses everything else in the main thread so it won't show multiple dialogs at a time, just on after the other. Could this m=be as simple as creating a new JFrame instead of using the JOptionPane?
推荐答案
根据 docs :
JOptionPane创建模态的JDialog。要创建非模态对话框,必须直接使用JDialog类。
JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
上面的链接显示了一些创建对话框的例子。
The link above shows some examples of creating dialog boxes.
另一个选择是在自己的线程中启动JOptionPane,如下所示:
One other option is to start the JOptionPane in its own thread something like this:
Thread t = new Thread(new Runnable(){
public void run(){
JOptionPane.showMessageDialog(null, "Hello");
}
});
t.start();
这样,即使模态对话框已启动,程序的主线程也会继续。
That way the main thread of your program continues even though the modal dialog is up.
这篇关于显示“JOptionPane.showMessageDialog”不停止执行流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!