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

发布于 2024-10-13 02:08:27 字数 189 浏览 3 评论 0原文

我的程序以 JFrame 中带有文本字段的图片开始。我希望当用户输入 start 时,它会关闭图片 JFrame 并使用主程序打开另一个 JFrame。我已经在图像框架上尝试过

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

但它关闭了所有窗口。

My program starts with a picture with a textfield in a JFrame. I want when the user types start it closes the picture JFrame and opens another JFrame with the main program. I've tried

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

on the Image frame but it closes all the windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

虚拟世界 2024-10-20 02:08:27

方法JFrame.setVisible可用于根据参数隐藏或显示JFrame,而JFrame.dispose实际上会通过关闭并释放框架来“销毁”框架增加它使用的资源。在这里,如果您打算重新打开相框,则可以在相框上调用 setVisible(false) ;如果您不打算打开相框,则可以在相框上调用 dispose()再次,这样你的程序就可以释放一些内存。然后,您可以在主框架上调用 setVisible(true) 使其可见。

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. 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. Then you would call setVisible(true) on the main frame to make it visible.

仄言 2024-10-20 02:08:27

您也可以使用此代码

例如,

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

you also can use this code

for example

  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
提笔落墨 2024-10-20 02:08:27

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

Maybe if you set the picture JFrame's default close operation to something besides EXIT_ON_CLOSE, perhaps DISPOSE_ON_CLOSE, you can prevent your application from closing before the second JFrame appears.

太阳男子 2024-10-20 02:08:27

我正在寻找同样的东西,发现使用“this”是最好和最简单的选择。
您可以使用以下代码:
this.dispose();

I was searching for the same thing and found that using "this" is the best and easiest option.
you can Use the following code:
this.dispose();

难如初 2024-10-20 02:08:27

这篇文章有点旧,但仍然如此。

如果您像这样初始化表单:

JFrame firstForm = new JFrame();

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

firstForm.setVisible(true);

例如通过按钮创建或打开另一个表单:

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));

这将在不退出程序的情况下处理并销毁第一个窗口。
关键是设置setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
它还引发事件(我已经使用 WindowClosing 事件对其进行了测试)。

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).
It also raises the events (I've tested it with the WindowClosing event).

故事灯 2024-10-20 02:08:27

这是我对这个问题的解决方案:

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 ");
    }
}

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 ");
    }
}
梅窗月明清似水 2024-10-20 02:08:27

对于 netbeans,使用当前对象的引用和 setVisible(false);
例如

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
}                                     

For netbeans use the reference of the current Object and 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
}                                     
爱要勇敢去追 2024-10-20 02:08:27

你也可以使用这个:

opens_frame frameOld= new opens_frame();
            frameOld.setVisible(true);
            Closing_Frame.setVisible(false);
            Closing_Frame.dispose();

you also can use this :

opens_frame frameOld= new opens_frame();
            frameOld.setVisible(true);
            Closing_Frame.setVisible(false);
            Closing_Frame.dispose();
轻许诺言 2024-10-20 02:08:27
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

}
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

}
三生一梦 2024-10-20 02:08:27

首先调用它

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

First call it

new Window().nextjframe.setVisible(true);
thisjframe.setVisible(false);
深巷少女 2024-10-20 02:08:27
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();
    }
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();
    }
且行且努力 2024-10-20 02:08:27

这就是我在关闭另一个 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);

This is what i came up for opening a new jframe while closing the other one:

JFrame CreateAccountGUI = new JFrame();
CreateAccountGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CreateAccountGUI.setSize(800, 600);
CreateAccountGUI.setLocationRelativeTo(null);
CreateAccountGUI.setVisible(true);   
this.setVisible(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文