简体   繁体   English

从父 jdialog 打开时,子 JDialog 显示为空白

[英]Child JDialog appears blank when opened from parent jdialog

My swing code is as below.我的 swing 代码如下。 I have to use setAlwaysOnTop(true) for all jdialogs for some requirement in the project.对于项目中的某些要求,我必须对所有 jdialogs 使用 setAlwaysOnTop(true)。 If i use so the child dialogs which is launched from parent dialogs will appear blank until it is resized with mouse manually.如果我这样使用,则从父对话框启动的子对话框将显示为空白,直到使用鼠标手动调整大小。

Please let me know if this can be resolved in any way.请让我知道这是否可以通过任何方式解决。

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Test {
    static JFrame f;
    public static void main(String[] args)
    {
        Test t = new Test();
        t.getFrame().show();
    }
    public JFrame getFrame(){
        f = new JFrame("frame");
        JPanel p = new JPanel();
        JButton b = new JButton("click");
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                String s = e.getActionCommand();
                if (s.equals("click")) {
                    JDialog d = new JDialog(f, "dialog Box");
                    d.setLayout( new FlowLayout() );
                    JButton b1 = new JButton ("OK");
                    d.add( new JLabel ("Click button to continue."));
                    d.add(b1);
                    d.setSize(300,300);
                    d.setAlwaysOnTop(true);
                    b1.addActionListener(new ActionListener()
                    {
                        public void actionPerformed( ActionEvent e1 )
                        {
                            String s1 = e1.getActionCommand();
                            if (s1.equals("OK")) {
                                JDialog d1= new JDialog(f, "child dialog Box", Dialog.ModalityType.DOCUMENT_MODAL);
                                JLabel l1 = new JLabel("this is a child dialog box");
                                d1.add(l1);
                                d1.setAlwaysOnTop(true);
                                d1.setSize(200, 200);
                                d1.setVisible(true);
                            }
                        }
                    });
                    d.setVisible(true);
                }
            }
        });
        p.add(b);
        f.add(p);
        f.setSize(400, 400);
        return f;
    }
}

When the dialog is launched对话框启动时

对话框启动时

When it is resized with mouse manually用鼠标手动调整大小时

用鼠标手动调整大小时

The owner of JDialog d1 is not the JFrame f but the other JDialog d . JDialog d1的所有者不是JFrame f而是另一个JDialog d
Change this line of your code:更改这行代码:

JDialog d1= new JDialog(f, "child dialog Box", Dialog.ModalityType.DOCUMENT_MODAL);

to this:对此:

JDialog d1= new JDialog(d, "child dialog Box", Dialog.ModalityType.DOCUMENT_MODAL);

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

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