获取对JOptionPane静态方法创建的对象的引用 [英] Getting hold of a reference to the object created by JOptionPane static methods

查看:238
本文介绍了获取对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!

推荐答案

静态 showXXXDialog()方法只是为了方便。如果您查看 JOptionPane 的源代码,您会发现实际上是基于 JOptionPane 对象创建的在您指定的选项上,然后调用 JOptionPane.createDialog(...)。在不同位置显示消息对话框的一种方法是:

The static showXXXDialog() methods are just for convenience. If you look at the source code for JOptionPane, you'll find that in actuality, a JOptionPane object is created based on the options you specify and then JOptionPane.createDialog(...) is called. One method to show your message dialog at a different position is:

JOptionPane pane = new JOptionPane("Message", JOptionPane.WARNING_MESSAGE,
        JOptionPane.DEFAULT_OPTION);
JDialog dialog = pane.createDialog("TITLE");
dialog.setLocation(0, 0);
dialog.setVisible(true);

// dialog box shown here

dialog.dispose();
Object selection = pane.getValue();

将参数组合到 JOptionPane 构造函数和 JOptionPane 设置方法,你可以做任何你用静态方法做的事情,另外你可以访问 JDialog 对象本身。

With a combination of parameters to the JOptionPane constructor, and JOptionPane set methods, you can do anything you would have done with the static methods, plus you have access to the JDialog object itself.

已编辑 :(为OP添加输入对话框示例)

EDITED: (to add example of input dialog for OP)

JOptionPane pane = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE,
        JOptionPane.OK_CANCEL_OPTION, null, null, null);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Title");
dialog.setLocation(0, 0);
dialog.setVisible(true);

String str = (String) pane.getInputValue();

这篇关于获取对JOptionPane静态方法创建的对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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