简体   繁体   English

从JPanel实例化JDialog

[英]Instantiate JDialog from JPanel

I've got a JPanel , which I want to respond to a mouse click and then open a JDialog . 我有一个JPanel ,我想响应鼠标点击然后打开一个JDialog The JDialog constructor needs an instance of JFrame and not JPanel - how do I work around this? JDialog构造函数需要一个JFrame实例而不是JPanel - 我该如何解决这个问题?

You should really try to attach the JDialog to a parent Dialog or Frame, especially if you want it modal (by passing a parent Window, the dialog will be attached to your Window and bringing the parent will bring the child dialog as well). 您应该尝试将JDialog附加到父对话框或框架,特别是如果您想要它是模态的(通过传递父窗口,对话框将附加到您的窗口并且使父对象也将带来子对话框)。 Otherwise, the user experience can really go wrong: lost dialogs, blocking windows without seeing the modal dialog, etc... 否则,用户体验确实会出错:丢失对话框,阻止窗口而不看模态对话框等...

To find your JPanel parent Window, all you need is this code: 要找到您的JPanel父窗口,您只需要以下代码:

JPanel panel = new JPanel();
Window parentWindow = SwingUtilities.windowForComponent(panel); 
// or pass 'this' if you are inside the panel
Frame parentFrame = null;
if (parentWindow instanceof Frame) {
    parentFrame = (Frame)parentWindow;
}
JDialog dialog = new JDialog(parentFrame);
...

If you don't know if you are in a Frame or Dialog, make the "instanceof" test for both classes. 如果您不知道自己是否在Frame或Dialog中,请对这两个类进行“instanceof”测试。

Using the parameter free constructor will make the dialog owner-less. 使用参数free构造函数将使对话无所有者。 I think that the best thing to do would be to make the Frame that owns your Panel the owner of the dialog. 我认为最好的办法是让拥有Panel的Frame成为对话框的拥有者。

By that, I mean that you should use the getParent() from your JPanel to find its owner and then send this object found as the owner of your JFrame. 这样,我的意思是你应该使用JPanel中的getParent()来查找它的所有者,然后发送这个作为你的JFrame所有者的对象。

A crude code for that would be 原始代码就是这样

java.awt.Container c = myPanel.getParent();  
while (!(c instanceof javax.swing.JFrame) && (c!=null)) {  
        c = c.getParent();  
}  
if (c!=null) { 
    JFrame owner=(javax.swing.JFrame) c;  
    JDialog myDialog=new JDialog(owner);
}

I have not tested this code, but it is good enought for you to understand the idea. 我没有测试过这段代码,但是你理解这个想法是件好事。

If you decided to go with a JOptionPane, you could add a MouseListener to the JPanel with a mouseAdapter inner class to handle mouseClicked events. 如果你决定使用JOptionPane,你可以使用mouseAdapter内部类将一个MouseListener添加到JPanel来处理mouseClicked事件。 You would have to declare the panel final in order to access the panel from within the inner class. 您必须声明面板final才能从内部类中访问面板。

final JPanel testPanel = new JPanel();

testPanel.addMouseListener(new MouseAdapter(){
     public void mouseClicked(MouseEvent e)
     {           
         JOptionPane.showMessageDialog(testPanel,"Title","InformationMessage",JOptionPane.INFORMATION_MESSAGE);

    }});//end of mouseClicked method

There's a constructor that don't need argument: 有一个不需要参数的构造函数:

JDialog dialog = new JDialog();

If what you want is to make the dialog modal, maybe you can get a static reference of you main JFrame, something like: 如果你想要的是使对话框模态,也许你可以得到你的主JFrame的静态引用,如:

JDialog dialog = new JDialog(MyMainJFrame.getInstance());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM