【发布时间】:2020-07-07 17:04:31
【问题描述】:
我的挥杆代码如下。对于项目中的某些要求,我必须对所有 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;
}
}
对话框启动时
用鼠标手动调整大小时
【问题讨论】: