applet包含以下代码:
public class TestApplet extends Applet {
public TestApplet() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDialog dialog = new JDialog();
dialog.setContentPane(new JLabel("Hello"));
dialog.setSize(new Dimension(300, 200));
dialog.setModal(true);
dialog.setVisible(true);
}
});
}}
当我在Windows 7上运行的InternetExplorer上打开它时,它可以工作:我更改浏览器选项卡,对话框始终保持在前面。
当我在运行于Red Hat Enterprise Linux Server版本6.3,Java 1.7.0_07-b10的Firefox ESR 10.0.5上打开它时,它立即进入浏览器窗口,我必须最小化浏览器以便再次找到它。
我需要做些什么才能使模态对话框始终位于Applet前面?
更新
将JDialog的创建更改为
JDialog dialog = new JDialog(javax.swing.SwingUtilities.getWindowAncestor(TestApplet.this));
没有区别。
答案 0 :(得分:1)
最后,在尝试了很多事情后,我想出了以下解决方法:
public class ModalDialog extends JDialog {
private boolean isClosing = false;
protected synchronized boolean isClosing() {
return isClosing;
}
protected synchronized void setClosing(boolean isClosing) {
this.isClosing = isClosing;
}
public ModalDialog() {
setSize(200, 300);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent arg0) {
if (isClosing()) {
System.out.println("Returned because dialog is already closing");
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
ModalDialog.this.setVisible(false);
ModalDialog.this.setVisible(true);
}
});
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Dialog is closing");
setClosing(true);
}
});
}
}