如何在Java applet中打开模态对话框?

时间:2010-10-17 23:25:54

标签: java applet dialog

我正在尝试在Applet前面显示模态对话框。

我当前的解决方案如下所示取出根框架:

Frame getMyParent() {
    Container parent = getParent();
    while (!(parent instanceof Frame)) {
        parent = ((Component)parent).getParent();
    }
    return (Frame)parent;
}

并按如下方式创建对话框:

public OptionsDialog(MainApplet applet, boolean modal) {
    super(applet.getMyParent(), "options", modal);
    // ....

然而,通常情况下会显示模式对话框 框架,但模态行为可以正常工作。

如何解决这个问题?

理想情况下,这应该适用于Java 1.5及更高版本。

3 个答案:

答案 0 :(得分:4)

JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(200, 200);
dialog.setVisible(true);

答案 1 :(得分:3)

Frame f =(Frame)SwingUtilities.getAncestorOfClass(Frame.class,parentWindow); 新的JDialog(f,true);

(来源= http://kb.trisugar.com/node/7613) 适用于parentWindow = sun.plugin2.main.client.PluginEmbeddedFrame

答案 2 :(得分:2)

使用 null 而不是applet.getMyParent()