我正在写java小程序。我想打开一个新框架,内容实际存储在我的 Applet 中。我正在通过按钮打开新框架:
openInNew.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
createDialog();
}
});
//this Function retrieves Frame for applet
public Frame getParentFrame( Component child )
{
Container c = child.getParent();
while ( c != null )
{
if ( c instanceof Frame )
{
return ( Frame ) c;
}
c = c.getParent();
}
return null;
}
private void createDialog()
{
Frame f=getParentFrame(openInNew); //openInNew is a button to Open a JDialog
frame = new AppletFrame("NEW FRAME",jContentPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
这是 的构造函数AppletFrame
:
public AppletFrame(String string, JPanel jContentPane, Frame f) {
super(f,string);
this.setContentPane(jContentPane);
this.setSize(790, 650);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
removeAll();
FileListViewer.destroyFrame();
}
});
}
public static void destroyFrame()
{
jContentPane= (JPanel) frame.getContentPane();
jContentPane.repaint(); /*this one should repaint the Applet View, but the result
* is still the same. jContentPane is not null,
* it is filled with acutal components. */
jContentPane.setVisible(true);
frame.dispose();
frame.setVisible(false);
frame=null;
}
我的问题是这jConentPane
是一个引用,所以当我打开我的AppletFrame
对象时,我的基本 Applet 框架中没有存储任何内容。我想再次设置jContentPane
为FileListViewer
,但我不能在 static 中引用非静态方法destroyFrame()
。