如何制作一个可调整大小的 JDialog?
我正在使用 JOptionPane.showOptionDialog 来显示 JDialog。我想知道如何:
- 设置对话框的尺寸(目前我在给定面板上使用 setPreferredSize() 方法,但我知道不应使用此类方法)。
- 使显示的对话框可调整大小。
我的代码如下所示:
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:
- 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).
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 HierarchyListener 访问 JOptionPane 的方法也有效:
http://blogs.oracle.com /scblog/entry/tip_making_joptionpane_dialog_ressized
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
为什么需要设置首选尺寸?如果正确构建面板,面板应该以其首选尺寸显示。
阅读 JOptionPane API。搜索“直接使用”。它向您展示了如何直接访问选项窗格使用的对话框,并且您可以
使用第二种方法为什么要设置大小?再次只需 pack() 对话框,它将以面板首选大小显示。
拿不到返回值是什么意思?您可以访问自定义面板的任何方法。因此,当您再次获得控制权时,只需调用 getXXX() 方法即可。只需确保对话框是模态的,并且 setVisible(true) 之后的代码将阻塞,直到对话框关闭。
Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.
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.
如果您想完全采用第二种方法,则必须在某处创建并放置您自己的“是”和“否”按钮(因为原始 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.