import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TwoWindow
{
public static void main(String[] args)
{
TwoWindowFrame frame=new TwoWindowFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class TwoWindowFrame 
extends JFrame
{
public TwoWindowFrame()
{
setTitle("Pop window");
setSize(300,300); JButton button=new JButton("Pop it");
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
p.show();

}
}); Container contentPane=getContentPane();
contentPane.add(button,BorderLayout.NORTH);
} PopWindow p=new PopWindow();
}class PopWindow 
extends JFrame
{
public PopWindow()
{
setTitle("I am a pop window");
setSize(100,100);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
show();
}
}
//好像不能把窗体放进cache!!!

解决方案 »

  1.   

    谢谢,我想做的是打开新窗体时关闭(应该是放进cache,我理解是隐藏)原先的窗体。往回调用时不使用new方法,直接把原先窗体调出来,同时关闭当前窗体(应该是EXIT_ON_CLOSE吧)。这个额外加分。
      

  2.   

    TwoWindowFrame.setvisible(true);显示,false为隐藏。具体的窗口是否是放入cache里,都与你没有太大关系(这些是垃圾收集器做的事)。this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);窗体被关闭
      

  3.   

    用以下代码来控制窗口关闭,实现你想要的动作:addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            //这里写入你想要进行的动作
        }
    });
    pack();
      

  4.   

    maybe peacock_king is mainly for customizing your window closing event handlers (for example, when you want to save some preferences when closing window). 
    If you just simply want to realize what you require in the topic, then it's simply as followed,TwoWindowFrame.setvisible(true);as stated by newman0708.
      

  5.   

    tks.我不能在这个贴子上添加分数吗?我还是没有找到。本来想给这个贴子追加200分的,不行就重新开贴子了。
    问问题先,窗体之间的传值怎么做?