简体   繁体   English

Java:如何在打开另一个 JFrame 的同时关闭另一个?

[英]Java: How do I close a JFrame while opening another one?

My program starts with a picture with a textfield in a JFrame.我的程序从 JFrame 中带有文本字段的图片开始。 I want when the user types start it closes the picture JFrame and opens another JFrame with the main program.我想在用户类型启动时关闭图片 JFrame 并使用主程序打开另一个 JFrame。 I've tried我试过了

processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

on the Image frame but it closes all the windows.在图像框架上,但它关闭了所有 windows。

The method JFrame.setVisible can be used to hide or display the JFrame based on the arguments, while JFrame.dispose will actually "destroy" the frame, by closing it and freeing up resources that it used. JFrame.setVisible方法可用于根据参数隐藏或显示 JFrame,而JFrame.dispose实际上会“销毁”该框架,方法是关闭它并释放它使用的资源。 Here, you would call setVisible(false) on the picture frame if you intend to reopen it, or call dispose() on the picture frame if you will not be opening it again, so your program can free some memory.在这里,如果您打算重新打开相框,您可以在相框上调用setVisible(false) ,如果您不想再次打开相框,则在相框上调用dispose() ,这样您的程序就可以释放一些内存。 Then you would call setVisible(true) on the main frame to make it visible.然后,您将在主框架上调用setVisible(true)以使其可见。

you also can use this code您也可以使用此代码

for example例如

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

也许如果您将图片 JFrame 的默认关闭操作设置为EXIT_ON_CLOSE之外的其他内容,也许是DISPOSE_ON_CLOSE ,您可以防止您的应用程序在第二个 JFrame 出现之前关闭。

Here is my solution to this problem:这是我对这个问题的解决方案:

public void actionPerformed(ActionEvent e) {
    String userName =  textField.getText();
    String password = textField_1.getText();
    if(userName.equals("mgm") &&  password.equals("12345")) {            
         secondFrame nF = new secondFrame();

         nF.setVisible(false);
         dispose();          
    }   
    else 
    {
        JOptionPane.showMessageDialog(null, " Wrong password ");
    }
}

This post is a bit old but nevertheless.这篇文章有点旧,但仍然如此。

If you initialize the form like that:如果您像这样初始化表单:

JFrame firstForm = new JFrame();

firstForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
firstForm.setSize(800, 600);
firstForm.setLocationRelativeTo(null);

firstForm.setVisible(true);

And for instance create or open another form by a button:例如,通过按钮创建或打开另一个表单:

JFrame secondForm = new JFrame();

secondForm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
secondForm.setSize(800, 600);
secondForm.setLocationRelativeTo(null);

secondForm.setVisible(true);

this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

This will dispose and destroy the first window without exiting the program.这将在不退出程序的情况下处理并销毁第一个窗口。
The key is to set setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) .关键是设置setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
It also raises the events (I've tested it with the WindowClosing event).它还会引发事件(我已经使用WindowClosing事件对其进行了测试)。

you also can use this :你也可以使用这个:

opens_frame frameOld= new opens_frame();
            frameOld.setVisible(true);
            Closing_Frame.setVisible(false);
            Closing_Frame.dispose();
private void closeTheCurrentFrameAndOpenNew(java.awt.event.ActionEvent evt){

 dispose();//To close the current window

 YourClassName closeCurrentWindow = new YourClassName();
 closeCurrentWindow.setVisible(true);//Open the new window

}

I was searching for the same thing and found that using "this" is the best and easiest option.我正在寻找同样的东西,发现使用“this”是最好和最简单的选择。 you can Use the following code: this.dispose();您可以使用以下代码: this.dispose();

For netbeans use the reference of the current Object and setVisible(false);对于 netbeans,使用当前对象的引用和setVisible(false); for example例如

private void submitActionPerformed(java.awt.event.ActionEvent evt)
{                                
    // TODO add your handling code here:
    this.setVisible(false);//Closing the Current frame
    new login().setVisible(true);// Opening a new frame
}                                     

First call it首先调用它

new Window().nextjframe.setVisible(true);
thisjframe.setVisible(false);

this is what i caame up for opening a new jframe wile closing the other one JFrame CreateAccountGUI = new JFrame();这就是我打开一个新的 jframe 同时关闭另一个 JFrame CreateAccountGUI = new JFrame();

     CreateAccountGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      CreateAccountGUI.setSize(800, 600);
      CreateAccountGUI.setLocationRelativeTo(null);
        CreateAccountGUI.setVisible(true);   
       this.setVisible(false);
if(username.equals("gaffar")&&password.equals("12345"))
    {
    label.setText("Be ready to continue");
    //Start of 2nd jframe
    NewJFrame1 n=new NewJFrame1();
     n.setVisible(true);
     //Stop code for ist jframe
     NewJFrame m=new NewJFrame();
     m.setVisible(false);
     dispose();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM