Applet中显示模式对话框

首先,我们看一下Applet的父级容器。如下:
|--> plugin.viewer.frame.IExplorerEmbeddedFrame
     |
     |--> plugin.AppletViewer
          |
          |--> javax.swing.JApplet

JDialog的父窗口只能是Frame或者Dialog。而Applet只是Panel的子类。因此,在Applet中,不能用new Dialog(applet, true)来创建一个模式对话框。只能通过获取Applet的上级Frame容器,才能成功创建。

/**
 * 取得父窗口。
 * 
 * @param compOnApplet compOnApplet为applet上的任意一个组件
 * @return Applet的父窗口
 */
public Frame getParentWindow(Component compOnApplet) {
	Container c = compOnApplet.getParent();
	while (c != null) {
		if (c instanceof Frame)
			return (Frame) c;
		c = c.getParent();
	}
	return null;
}


然后像下面这样调用
// this为JApplet对象
JDialog dialog = new JDialog(getParentWindow(this), true);

你可能感兴趣的:(java,C++,c,swing,C#)