How to go back to a JFrames in Java

前端 未结 3 1529
别跟我提以往
别跟我提以往 2021-01-06 06:45

I have a button in a JFrame, if pressed, it takes us to another frame. I used this code:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt         


        
3条回答
  •  半阙折子戏
    2021-01-06 07:12

    If you pass your MainForm to the SecondForm class (for example using a constructor parameter) the SecondForm instance can make the original MainForm instance visible again instead of creating a new one.

    For example

    public class SecondForm extends JFrame{
     private final MainForm mainForm;
     public SecondForm( MainForm form ){
       mainForm = form;
     }
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      mainForm.setVisible(true);
      setVisible(false);
      dispose();
     }  
    }
    

    and in your MainForm class

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
     SecondForm secondform = new SecondForm( this );
     secondform.setVisible(true);
     setVisible(false);
     dispose();
    }
    

提交回复
热议问题