简体   繁体   English

单击JButton后关闭jFrame

[英]Closing jFrame after clicking JButton

I have designed two JFrames in NetBeans. 我在NetBeans中设计了两个JFrame。

When I click the "rules" button (ie placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want). 当我单击“规则”按钮(即放在JFrame1上)时,它将打开第二个JFrame(但是JFrame2在JFrame1的窗口上打开,这是我不想要的)。 In the second JFrame there is a "close" button. 在第二个JFrame中,有一个“关闭”按钮。 But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2. 但是,当我单击此按钮时,我希望打开JFrame1并且它也可以工作,但是JFrame2实际上没有关闭,并且JFrame1出现在JFrame2上方。

In short the main form is JFrame1. 简而言之,主要形式是JFrame1。 When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (ie JFrame1) is lauched but it is launched over JFrame2. 当我单击JFrame1中的“规则”按钮时,它将在JFrame1之上打开JFrame2,而在JFrame2中单击该主窗体(即JFrame1)时会出现一个“关闭”按钮,但它会通过JFrame2启动。

The scenerio is JFframe1 -> JFrame2 -> JFrame1 场景是JFframe1-> JFrame2-> JFrame1

Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa. 现在我的问题是单击“规则”按钮后,应关闭JFrame1,并在屏幕上显示JFrame2,反之亦然。

Assuming your button has an actionListener, after clicking the "rules button" put in: 假设您的按钮上有一个actionListener,然后单击“规则按钮”:

      JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame

And then reverese them for the opposite reaction 然后敬重他们的相反反应

Somethig like this should be on the constructor or method which create JFrame2: 这样的东西应该在创建JFrame2的构造函数或方法上:

btnCancel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //call another method in the same class which will close this Jframe
        CloseFrame();
    }
});

It's method which should close JFrame2 是应该关闭JFrame2的方法

public void CloseFrame(){
    super.dispose();
}

I'm not an expert by any means, however, I ran into this problem as well. 无论如何我都不是专家,但是我也遇到了这个问题。 If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame. 如果将第二个JFrame设置为隐藏,则单击“取消”时,它将关闭第二个JFrame。

//this is the code for the "cancel" button action listener 
public void actionPerformed(ActionEvent e) {
    setVisible(false);//hides the second JFrame and returns to the primary

this worked for me ( Frame1 Called RegScreen and Frame2 Called MainScreen ): 这为我工作( Frame1称为RegScreenFrame2称为MainScreen ):

RegScreen.this.setVisible(false);

new MainScreen().setVisible(true);

Hope that this helps :) Regscreen was the original frame open at startup. 希望对您Regscreen帮助:) Regscreen是启动时打开的原始框架。

If this doesn't work, try this 如果这不起作用,请尝试此

JFrame1.dispose();  //Remove JFrame 1
      JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
  1. Have a MainClass with a main() method. 有一个带有main()方法的MainClass。
  2. Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. 让具有main()方法的MainClass封装您的JFrame1和JFrame2参考变量。 Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason. 除非有特殊原因,否则不要让JFrame1或JFrame2包含main()。
  3. After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods. 在一个JFrame对象中单击某些内容后,实例化/显示另一个JFrame对象,并通过您的MainProgram.JFrame对象方法对其进行处理。

Example: 例:

    //btn event inside 1st JFrame/window
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         MainProgram.openResultsForm();  //MainProgram opens 2nd window
         MainProgram.queryEntryForm.dispose();   //MainProgam closes this,
                                                 //the 1st window
    }

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

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