Swing对话框如何工作? [英] How can Swing dialogs even work?

查看:93
本文介绍了Swing对话框如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你在Swing中打开一个对话框,例如一个JFileChooser,它就像这个伪代码:

If you open a dialog in Swing, for example a JFileChooser, it goes somewhat like this pseudocode:

swing event thread {
  create dialog
  add listener to dialog close event {
    returnValue = somethingFromDialog
  }
  show dialog
  (wait until it is closed)
  return returnValue
}

我的问题是:这怎么可能有效?如您所见,线程等待返回,直到对话框关闭。这意味着Swing事件线程被阻止。然而,人们可以与对话框进行交互,AFAIK需要这个线程才能运行。

My question is: how can this possibly work? As you can see the thread waits to return until the dialog is closed. This means the Swing event thread is blocked. Yet, one can interact with the dialog, which AFAIK requires this thread to run.

那么它是如何工作的?

推荐答案

这是AWT的主题,而不是Swing的。

It's the AWT's thread, not Swing's.

无论如何,AWT在展示。被阻止的窗口的输入事件被阻止。重新启动事件,事件到未阻止的窗口和一般事件照常发送。

Anyway, AWT runs the dispatch loop within the show. Input events to blocked windows are blocked. Repaint events, events to unblocked windows and general events are dispatched as usual.

您可以通过添加以下行来看到:

You can see this either by adding the line:

 Thread.dumpStack();

进入模态对话框的偶数处理,或者更容易从命令行使用 jstack 或在命令窗口中使用 ctrl-\ / ctrl-break 应用程序。

into the even handling for the modal dialog, or more easily from the command line with jstack or use ctrl-\/ctrl-break in the command window of the application.

Foxtrot 库滥用此功能提供更多程序(而不是事件驱动)模型。当从应用程序EDT调用时,WebStart / Java PlugIn也使用它来为JNLP服务和其他服务提供对话框。

The Foxtrot library abuses this to provide a more procedural (as opposed to event-driven) model. It's also used by WebStart/Java PlugIn to provide dialogs for JNLP services and others when called from the application EDT.

这篇关于Swing对话框如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆