如何将JOptionPane转换为JDialog [英] How to convert a JOptionPane to a JDialog

查看:36
本文介绍了如何将JOptionPane转换为JDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JOptionPage转换为JDialog,因为我需要摆脱那个古怪的确定"按钮.下面是代码...

I need to convert a JOptionPage to a JDialog, because I need to get rid of that quirky "OK" button. Below is the code...

JOptionPane.showMessageDialog(null, Interface, caption, JOptionPane.WARNING_MESSAGE, icon);

界面是我的GUI,标题"是标题,然后是警告消息,然后是自定义图标.

Interface is my GUI, "caption" is the title, then I have a warning message, then I have my custom icon.

使用给定的信息甚至可能无法实现,但我确实需要摆脱该问题.任何帮助将不胜感激.

This might not even be possible with the given info, but I really need to get rid of that OK. Any help would be appreciated.

推荐答案

为什么不简单地创建一个JDialog并将您的接口对象放入其contentPane中,然后打包并显示它.很简单.

Why not simply create a JDialog and put your interface object into its contentPane, then pack it and display it. Simple.

您声明:

很明显,JOptionPane很小,但是要做一个JDialog,我必须创建一个Window和更多的东西. GUI已经创建,我可以简单地将其导入到JOptionPane中.我相信JDialog无法做到这一点.

Obviously JOptionPane is small, but to do a JDialog I'd have to create a Window and more stuff. The GUI is already created, and I can simply import that into the JOptionPane. Can't do that for a JDialog I believe.

我很困惑.您所说的"GUI"是什么意思?同样,如果它是一个JPanel,只需将其放入JDialog中即可.

I'm confused. What do you mean by "the GUI"? Again, if it is a JPanel, again, simply put it into your JDialog.

例如在ActionListener中

For instance if in an ActionListener

public void actionPerformed(ActionEvent e) {
  // assuming this is being called from within a JFrame named myFrame
  JDialog dialog = new JDialog(myFrame, "Dialog Title", ModalityType.APPLICATION_MODAL);
  dialog.getContentPane().add(interface);
  dialog.pack();
  dialog.setLocationRelativeTo(myFrame);
  dialog.setVisible(true);
}


另一个选择是使用JOptionPane构造函数,并在不显示它的情况下创建它,然后将其转换为JDialog,因为JOptionPane API向您展示了如何操作,但是您可能仍然需要使用OK或类似的按钮来处理.


Another option is to use the JOptionPane constructor, and create it without showing it, and then convert it to a JDialog as the JOptionPane API shows you how to do, but you may still have the OK or similar button to deal with.

这篇关于如何将JOptionPane转换为JDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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