Applet中显示模式对话框

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

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

Java代码 复制代码 收藏代码
  1. /**  
  2.  * 取得父窗口。  
  3.  *   
  4.  * @param compOnApplet compOnApplet为applet上的任意一个组件  
  5.  * @return Applet的父窗口  
  6.  */  
  7. public Frame getParentWindow(Component compOnApplet) {   
  8.     Container c = compOnApplet.getParent();   
  9.     while (c != null) {   
  10.         if (c instanceof Frame)   
  11.             return (Frame) c;   
  12.         c = c.getParent();   
  13.     }   
  14.     return null;   
  15. }  
/**
 * 取得父窗口。
 * 
 * @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;
}



然后像下面这样调用

Java代码 复制代码 收藏代码
  1. // this为JApplet对象   
  2. JDialog dialog = new JDialog(getParentWindow(this), true);  
posted @ 2011-08-22 21:02  为谁  Views(484)  Comments(0Edit  收藏  举报