'JOptionPane.showConfirmDialog() 这里的第一个参数是什么? [英] What is first argument in 'JOptionPane.showConfirmDialog() here?
问题描述
这是下面的代码片段,我想使用 JOptionPane.showConfirmDialog()
通过将其父项设置为 resultsTablePanel
来显示对话框:
Here is the below code snippet, where i would like to display the dialog using JOptionPane.showConfirmDialog()
by setting its parent as resultsTablePanel
:
public class SearchResultsTablePanel extends JPanel{...}
public class DefaultSearchListener{
private SearchResultsTablePanel resultsTablePanel = null;
public void f(X x) {
int response = JOptionPane.showConfirmDialog(
resultsTablePanel,
"hai",
"Warning", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
// do something
} else if (response == JOptionPane.YES_OPTION) {
// do something
} else if (response == JOptionPane.CLOSED_OPTION) {
// do something
}
}
}
我的问题:
对于 java swing api JOptionPane.showConfirmDialog()
,我需要将 resultsTablePanel
作为第一个参数传递吗?
For java swing api JOptionPane.showConfirmDialog()
,
Do i need to pass resultsTablePanel
as first argument?
或
我需要将 JOptionPane.getframeforcomponent(resultsTablePanel)
作为第一个参数传递吗?
Do i need to pass JOptionPane.getframeforcomponent(resultsTablePanel)
as first argument?
推荐答案
这并不重要.
作为处理的一部分,JOptionPane
调用 getWindowForComponent
参数,然后将其传递给底层 JDialog
(这稍微更通用).
As part of the processing, JOptionPane
invokes getWindowForComponent
on the argument before passing it through to the underlying JDialog
(which is slightly more general).
至于隐含的问题无论如何有什么区别?",这与模态有关.在这里,我强烈建议您阅读 Oracle 的模态指南.
As for the implicit question "What's the difference anyway?", well this has to do with modality. Here I would urge you to read Oracle's guide to modality.
JOptionPane
使用 JDialog
的默认模态类型,这是上面指南中的应用模态.这意味着对话框将阻止对应用程序中所有窗口的输入,除了以对话框为父窗口的窗口.因此,如果您打开 2 个具有相同父级的对话框 - 您会遇到麻烦,但如果一个对话框作为父级 - 那么子级拥有控制权,并且一旦关闭就会将它们转移给他们的父级.
JOptionPane
uses the default modality type for JDialog
, which is the Application-modal from the guide above. This means that the dialog will block input to all windows in the application, except windows that have the dialog as the parent. Hence, if you open 2 dialogs that have the same parent - you're in trouble, but if one has the other as the parent - then the child has controls and will transfer them to their parent once closed.
这篇关于'JOptionPane.showConfirmDialog() 这里的第一个参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!