我想隐藏新帧窗口出现时的第一帧

时间:2015-04-09 18:23:21

标签: java swing jframe awt

我希望在按下提交按钮后出现新窗口时隐藏上一个窗口框架,如何隐藏上一个窗口或关闭它而不按交叉按钮

enter code here
public static void main(String[] args) 
 {

        JFrame frame = new JFrame("Project Format Creator");

        JButton btn5 = new JButton("submit"); 
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints cst = new GridBagConstraints();

        cst.fill = GridBagConstraints.HORIZONTAL;
        cst.gridx = 0;
        cst.gridwidth = 1; 
        cst.weightx = 0.1;
        cst.gridy = 8;       //third row
        panel.add(btn5,cst);

      btn5.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            JFrame frame1 = new JFrame("Project Format Creator");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setSize(300,300);//int width int height
            frame1.getContentPane().add(panel);
            frame1.setVisible(true);

         }
     });



         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(300,300);//int width int height
         frame.getContentPane().add(panel);
         frame.setVisible(true);
} 

1 个答案:

答案 0 :(得分:1)

如果您要再次显示该JFrame,请使用Frame.setVisible(false);;如果您已完成,则使用Frame.dispose();

   public void actionPerformed(ActionEvent e)
     {
        frame.dispose(); // dispose the old frame
        JFrame frame1 = new JFrame("Project Format Creator");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(300,300);//int width int height
        frame1.getContentPane().add(panel);
        frame1.setVisible(true);
     }