如何制作一个可调整大小的 JDialog?

发布于 2024-12-07 17:08:04 字数 1157 浏览 0 评论 0原文

我正在使用 JOptionPane.showOptionDialog 来显示 JDialog。我想知道如何:

  1. 设置对话框的尺寸(目前我在给定面板上使用 setPreferredSize() 方法,但我知道不应使用此类方法)。
  2. 使显示的对话框可调整大小。

我的代码如下所示:

JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
                    panel,
                        "titel",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    options,
                    options[1]);

我知道我可以通过这种方式构建 JDialog 获得所需的结果:

JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);

最后一个解决方案的问题是我无法获得返回值。

编辑: 回应@camickr 的观察:

为什么需要设置首选尺寸?如果您构建面板 正确的是应该以其首选尺寸显示。

我不确定在这一点上我是否完全理解了 Swing。例如,问题是我通过 JDialog 显示使用 JFreeChart 构建的 ChartPanel。现在,我想该面板有它自己的首选尺寸,但我希望看到它更大。在不显式使用 setPreferredSize() 的情况下如何做到这一点?

阅读 JOptionPane API。搜索“直接使用”。它向您展示了如何 直接访问选项窗格使用的对话框,您可以

阅读它,但我找不到正确的方法来了解 JDialog 上按下了哪个按钮(确定或取消)。

I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:

  1. set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
  2. make the showed dialog resizable.

My code looks like:

JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
                    panel,
                        "titel",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    options,
                    options[1]);

I know that I could obtain the desired result building a JDialog this way:

JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);

The problem with the last solution is that I can't get the return value.

EDIT:
in response to @camickr observations:

Why do you need to set the preferred size? If you build the panel
properly is should be displayed at its preferred size.

I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?

Read the JOptionPane API. Search for "Direct Use". It shows you how to
directly access the dialog used by the option pane and you can

I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦在夏天 2024-12-14 17:08:04

使用 HierarchyListener 访问 JOptionPane 的方法也有效:

http://blogs.oracle.com /scblog/entry/tip_making_joptionpane_dialog_ressized

// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
    public void hierarchyChanged(HierarchyEvent e) {
        Window window = SwingUtilities.getWindowAncestor(pane);
        if (window instanceof Dialog) {
            Dialog dialog = (Dialog)window;
            if (!dialog.isResizable()) {
                dialog.setResizable(true);
            }
        }
    }
});

This hack using a HierarchyListener to get access to the JOptionPane's also works:

http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable

// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
    public void hierarchyChanged(HierarchyEvent e) {
        Window window = SwingUtilities.getWindowAncestor(pane);
        if (window instanceof Dialog) {
            Dialog dialog = (Dialog)window;
            if (!dialog.isResizable()) {
                dialog.setResizable(true);
            }
        }
    }
});
书间行客 2024-12-14 17:08:04
  1. 为什么需要设置首选尺寸?如果正确构建面板,面板应该以其首选尺寸显示。

  2. 阅读 JOptionPane API。搜索“直接使用”。它向您展示了如何直接访问选项窗格使用的对话框,并且您可以

使用第二种方法为什么要设置大小?再次只需 pack() 对话框,它将以面板首选大小显示。

拿不到返回值是什么意思?您可以访问自定义面板的任何方法。因此,当您再次获得控制权时,只需调用 getXXX() 方法即可。只需确保对话框是模态的,并且 setVisible(true) 之后的代码将阻塞,直到对话框关闭。

  1. Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.

  2. Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can

With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.

What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.

独闯女儿国 2024-12-14 17:08:04

如果您想完全采用第二种方法,则必须在某处创建并放置您自己的“是”和“否”按钮(因为原始 JDialog 只是一个空的可修改框架)。因此,您需要将 MouseListener 附加到两个按钮并处理单击事件。单击后,您将知道按下了哪个按钮,并且只需在对话框上调用 dispose() 即可将其关闭。

If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.

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