JOptionPane window 在后台打开
JOptionPane window opens in background
我正在开发一个 swing 应用程序,只是关于 JOptionPane.showMessageDialog()
的一个小问题困扰着我:
JOptionPane.showMessageDialog(null, "Record entered successfully");
如果我编写此代码,消息 window 会出现在我父框架的后面。
JOptionPane.showMessageDialog(this, "Record entered successfully");
而此代码会自动将 window 放置在父框架上。
问题是:在将 null
作为第一个参数实现时,我会在当前父框架的背景中获取消息,而如果我写 this
作为第一个参数,window 来自父框架。为什么会这样?
java 关键字 this
用于(在这种情况下)指代当前的 class - 所以您指的是您的 parent window.看到这个link,非常方便:
http://javapapers.com/core-java/explain-the-java-this-keyword/
方法中
showMessageDialog(Component parentComponent, Object message)
第一个参数设置对话框的父级:
parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null
, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
我假设该方法出现在 JFrame
class 中,在这种情况下,将 this
作为参数传递会将父组件设置为该框架。
我正在开发一个 swing 应用程序,只是关于 JOptionPane.showMessageDialog()
的一个小问题困扰着我:
JOptionPane.showMessageDialog(null, "Record entered successfully");
如果我编写此代码,消息 window 会出现在我父框架的后面。JOptionPane.showMessageDialog(this, "Record entered successfully");
而此代码会自动将 window 放置在父框架上。
问题是:在将 null
作为第一个参数实现时,我会在当前父框架的背景中获取消息,而如果我写 this
作为第一个参数,window 来自父框架。为什么会这样?
java 关键字 this
用于(在这种情况下)指代当前的 class - 所以您指的是您的 parent window.看到这个link,非常方便:
http://javapapers.com/core-java/explain-the-java-this-keyword/
方法中
showMessageDialog(Component parentComponent, Object message)
第一个参数设置对话框的父级:
parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be
null
, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
我假设该方法出现在 JFrame
class 中,在这种情况下,将 this
作为参数传递会将父组件设置为该框架。