0

Button actionlistener. (r is the call method for my class i call it above Run r= new Run();. it sets the window invisible but when it's supposed to get it back visible the program closes without any errors. Tried instead of setVisible(false); dispose(); but same problem.

about.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JFrame a=new JFrame("About");

            a.addWindowListener(new WindowListener(){
                @Override
                public void windowActivated(WindowEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void windowClosed(WindowEvent arg0) {


                }

                @Override
                public void windowClosing(WindowEvent arg0) {
                    r.gui.setVisible(true);

                }

                @Override
                public void windowDeactivated(WindowEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void windowDeiconified(WindowEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void windowIconified(WindowEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void windowOpened(WindowEvent arg0) {
                    r.gui.setVisible(false);

                }
            });

            a.setSize(400, 400);
            a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            a.setVisible(true);
            a.setLayout(null);

            JLabel lbl=new JLabel("This game was made by your lovely neighbourhood takisp22");
            lbl.setSize(500,50);
            lbl.setLocation(0,0);
            a.setLocation(100,50);
            a.add(lbl);
        }
    });

Other Class which runs the program:

    import javax.swing.JFrame;

public class Run {
    public static GameAim gui=new GameAim();
    public static void main(String[] args){

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setTitle("Aim Training");
        gui.setSize(1280, 800);
        gui.setVisible(true);
        gui.setLocation(100,50);
        gui.setResizable(false);

        gui.openFile();
        gui.readFile();
        gui.closeFile();
    }
}
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40

1 Answers1

2

Because of this:

gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

when you close the gui JFrame variable, the program exits.

Don't use this, but instead use JFrame.DISPOSE_ON_CLOSE. Having said this, please read: The Use of Multiple JFrames, Good/Bad Practice?. Your user is not going to like to have multiple windows shoved at them. Swap views instead with a CardLayout -- tutorial link.

We'll also need to discuss the evils of your use of a.setLayout(null); at some time.
;)

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373