我正在编写java applet。 我想打开一个新的Frame,其内容实际存储在我的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
,但我不能在静态destroyFrame()
中引用非静态方法。
答案 0 :(得分:3)
此处无需打开新的JFrame或其他Top Level Containers,而是使用CardLayout
如果您想移动一个JDialog toFront/toBack