Closing jFrame after clicking JButton

主宰稳场 提交于 2019-12-17 16:38:00

问题


I have designed two JFrames in NetBeans.

When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want). In the second JFrame there is a "close" button. 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.

In short the main form is 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 (i.e JFrame1) is lauched but it is launched over JFrame2.

The scenerio is 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.


回答1:


Assuming your button has an actionListener, after clicking the "rules button" put in:

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

And then reverese them for the opposite reaction




回答2:


Somethig like this should be on the constructor or method which create 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

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



回答3:


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.

//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



回答4:


this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):

RegScreen.this.setVisible(false);

new MainScreen().setVisible(true);

Hope that this helps :) Regscreen was the original frame open at startup.




回答5:


If this doesn't work, try this

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



回答6:


  1. Have a MainClass with a main() method.
  2. Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
  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.

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
    }


来源:https://stackoverflow.com/questions/18931261/closing-jframe-after-clicking-jbutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!