获取对 JOptionPane 静态方法创建的对象的引用

发布于 2024-10-09 12:49:57 字数 201 浏览 0 评论 0原文

我想知道是否可以获取对由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2024-10-16 12:49:57

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

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 对象本身。

编辑:(为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();

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();

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.

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();
尘曦 2024-10-16 12:49:57

JOptionPane 将使用给定的 parentComponent (第一个方法参数)来确定对话框的中心位置(例如在 javax.swing.JOptionPane.showMessageDialog(Component, Object) 中)

您可以尝试传入假组件将对话框定位到另一个位置,例如如下所示:

    JFrame frame = new JFrame("Test");
    frame.setLocation(100, 100);
    frame.setSize(500, 500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    // 'Invisible' fake component for positioning
    JWindow c = new JWindow();
    c.setSize(0, 0);
    c.setVisible(true);
    Point location = frame.getLocation();
    location.translate(200, 100);
    c.setLocation(location);

    JOptionPane.showInputDialog(c,"Foo");

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:

    JFrame frame = new JFrame("Test");
    frame.setLocation(100, 100);
    frame.setSize(500, 500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    // 'Invisible' fake component for positioning
    JWindow c = new JWindow();
    c.setSize(0, 0);
    c.setVisible(true);
    Point location = frame.getLocation();
    location.translate(200, 100);
    c.setLocation(location);

    JOptionPane.showInputDialog(c,"Foo");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文