我的挥杆代码如下。对于项目中的某些要求,我必须对所有jdialog使用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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句