将应用程序移动到其他帧然后再返回到上一帧

时间:2012-04-23 10:57:24

标签: java swing applet jframe jpanel

我正在编写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框架中没有任何内容存储。我想再次设置jContentPaneFileListViewer,但我不能在静态destroyFrame()中引用非静态方法。

1 个答案:

答案 0 :(得分:3)