SwingUtilites:如何从Java中的另一个线程返回值? [英] SwingUtilites: how to return values from another thread in Java?
问题描述
我正在尝试用Java编写应用程序. 为了使Swing正常工作,我做到了:
I am trying to make an application in Java. To make Swing work correctly, I did this:
public static void main(String[] array){
String outerInput;
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
// I want this string input.
String input = JOptionPane.showInputDialog(
null,"Stop ?", JOptionPane.QUESTION_MESSAGE);
});
// How can I get this input value in String outerInput?
}
我如何在主体中获得此输入字符串?
How would I get this input string in my main body?
推荐答案
我如何在主体中获得此输入字符串?
How would I get this input string in my main body?
您不会.您的主要"将调用一个Swing对话框,然后对结果进行某些处理的想法与图形用户界面的整个想法背道而驰.
You wouldn't. The idea that your "main" would invoke a Swing dialog box and then do something with the results is contrary to the entire idea of a graphical user interface.
在GUI中,您可以设计程序来处理一系列用户启动的事件.这些事件可能是完全异步的,例如典型的文字处理器的击键,选择和菜单选择.或者可以编写脚本,例如向导"的问答格式.
In a GUI, you design your program to deal with a series of user-initiated events. Those events may be completely asynchronous, such as the keystrokes, selection, and menu choices of your typical word processor. Or they may be scripted, such as the question-answer format of a "wizard."
假设您想做类似后者的事情,则可以按照以下顺序实现它:
Assuming that you want to do something like the latter, you would implement it using the following sequence:
- 用户启动某些操作,也许选择菜单选项.这被转换为
ActionListener
的调用,它决定它需要来自用户的更多输入. - 在事件分派线程上执行的
ActionListener
被允许对UI执行其想要的任何操作,例如显示对话框.该对话框可以是模式对话框,也可以是非模式对话框.在一种情况下,输出可用于原始侦听器,在另一种情况下,您需要编写一个新的侦听器以执行后续操作. - 一旦您有足够的信息,就可以选择调用后台操作.通常,您将有一个线程池来处理这些请求.您不会尝试在主"线程上执行请求;实际上,对于所有意图,主线程都不再运行.
- 操作完成运行后,它将使用
SwingUtilities.invokeLater()
将数据推回到事件分发线程.尽管可以在后台操作过程中使用invokeAndWait()
将结果发送到Swing,但这并不是一个好主意.而是创建一系列操作,最好是由用户轻松取消的一系列操作.
- The user initiates some action, perhaps selecting a menu choice. This is turned into an invocation of an
ActionListener
, which decides that it needs more input from the user. - The
ActionListener
, which is executed on the event dispatch thread, is permitted to do anything that it wants to the UI, such as displaying a dialog. That dialog may be modal or non-modal; in one case the output is available to the original listener, in the other you need to write a new listener to take subsequent action. - Once you have enough information, you may choose to invoke a background operation. You would typically have a thread-pool to service these requests. You would not attempt to perform the request on the "main" thread; in fact, for all intents the main thread is no longer running.
- When your operation completes running, it would push data back to the event dispatch thread using
SwingUtilities.invokeLater()
. While you could useinvokeAndWait()
to send results to Swing in the middle of your background operation, that's rarely a good idea. Instead, create a sequence of operations, preferably one that is easily canceled by the user.
在后台线程上启动操作的标准"方法是通过 SwingWorker .有其他选择.例如,您可以使用BlockingQueue
将操作发送到单个长时间运行的后台线程,并使用invokeLater()
返回结果.
The "standard" way to initiate operations on a background thread is via SwingWorker. There are alternatives; for example, you could use a BlockingQueue
to send operations to a single long-running background thread, and use invokeLater()
to return the results.
无论如何,有一个规则是您不想要打破:永远不要在事件分发线程上执行阻止操作.如果这样做,则您的应用程序会损坏.
Regardless, there's one rule that you do not want to break: never, ever, perform a blocking operation on the event dispatch thread. If you do that, then your application is broken.
这篇关于SwingUtilites:如何从Java中的另一个线程返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!