获取对 JOptionPane 静态方法创建的对象的引用
我想知道是否可以获取对由 JOptionPane 的静态方法之一(例如 showMessageDialog)创建的(JDialog?)对象的引用?我打算修改对话框在屏幕上出现的位置。更具体地说,我希望对话框默认显示在主应用程序窗口的左上角,而不是窗口的中心。因此,拥有对该对象的引用将使我能够使用 setLocation 来实现所需的效果...
任何建议将不胜感激!谢谢!
I wonder if it is possible to get hold of a reference to the (JDialog?) object created by one of those static methods of JOptionPane (e.g. showMessageDialog)? I intend to modify the position where the dialog appears on the screen. More specifically, I want the dialog to appear at the top-left corner of the main app window, instead of the centre of the window by default. So having a reference to the object would enable me to use setLocation to achieve the desired effect...
Any suggestion would be appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
静态
showXXXDialog()
方法只是为了方便。如果您查看JOptionPane
的源代码,您会发现实际上,一个JOptionPane
对象是根据您指定的选项创建的,然后是JOptionPane。 createDialog(...)
被调用。在不同位置显示消息对话框的一种方法是:通过组合
JOptionPane
构造函数和JOptionPane
设置方法的参数,您可以执行任何您想要执行的操作使用静态方法,此外您还可以访问JDialog
对象本身。编辑:(为OP添加输入对话框的示例)
The static
showXXXDialog()
methods are just for convenience. If you look at the source code forJOptionPane
, you'll find that in actuality, aJOptionPane
object is created based on the options you specify and thenJOptionPane.createDialog(...)
is called. One method to show your message dialog at a different position is:With a combination of parameters to the
JOptionPane
constructor, andJOptionPane
set methods, you can do anything you would have done with the static methods, plus you have access to theJDialog
object itself.EDITED: (to add example of input dialog for OP)
JOptionPane 将使用给定的
parentComponent
(第一个方法参数)来确定对话框的中心位置(例如在 javax.swing.JOptionPane.showMessageDialog(Component, Object) 中)您可以尝试传入假组件将对话框定位到另一个位置,例如如下所示:
The JOptionPane will use the given
parentComponent
(first method parameter) to determine where to center the dialog (for example in javax.swing.JOptionPane.showMessageDialog(Component, Object))You could try to pass in a fake component which positions the dialog to another location, for example like this: