简体   繁体   中英

Having difficulty when trying to return to previous JFrame

I am having difficulty when trying to return to previous JFrame . In FirstFrame, it has a parameter. In secondFrame, how can I back to firstFrame since it does not has parameter ?

I am pulling my hair out of this. Any help would be appreciated.

FirstFrame.java

public class FirstFrame extends JFrame
{
public FirstFrame(final String name)
{
 goToSecondFrame.addActionListener(new ActionListener()
              {
          public void actionPerformed(ActionEvent e)
            {
                SecondFrame sec= new SecondFrame();
                sec.createAndShowGui();
               sec.setVisible(true);
               setVisible(false);
               dispose();       
            }
              });
}
}

SecondFrame.java

 public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                createAndShowGui();
            });
        }

     static void createAndShowGui() {
            JFrame frame = new JFrame("Second Frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new Second());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

public Second()
{
 back.addActionListener(new ActionListener()
                  {
              public void actionPerformed(ActionEvent e)
                {
                  FirstFrame back = new FirstFrame(); // Getting error
                  back.setVisible(true);
                  setVisible(false);
                  dispose();         

                }
                  });
}

Define your frame first in the top of the class:

JFrame frame = new JFrame("Delete Admin");

and then do like this:

back.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
staffManagment back = new staffManagment(""); 
back.setVisible(true); 
setVisible(false); 
frame.dispose(); 
} 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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