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

Child JDialog appears blank when opened from parent jdialog

我的swing代码如下。对于项目中的某些要求,我必须对所有 jdialogs 使用 setAlwaysOnTop(true)。如果我这样使用,从父对话框启动的子对话框将显示为空白,直到用鼠标手动调整它的大小。

如果可以通过任何方式解决这个问题,请告诉我。

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;
    }
}

对话框启动时

手动用鼠标调整大小时

JDialog d1 的所有者不是 JFrame f 而是另一个 JDialog d.
更改您的这行代码:

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

对此:

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