Bit of help with JFrame window listener

Hey guys.
I’m new to the boards, and relatively new to Java compared to most :stuck_out_tongue:

I wrote a game, in a JFrame, as an application. Now, to change it to an applet, it works fine, running it in a browser. (Haven’t tried hosting it yet). The only problem is closing the window.
Here is the code for my windowListener part:

  JFrame ventana = new JFrame("RA Invaders");
  JPanel panel = (JPanel) ventana.getContentPane();
  setBounds(0, 0, Stage.WIDTH, Stage.HEIGHT);
  panel.setPreferredSize(new Dimension(Stage.WIDTH, Stage.HEIGHT));
  panel.setLayout(null);
  panel.add(this);
  ventana.setBounds(0, 0, Stage.WIDTH, Stage.HEIGHT);
  ventana.setVisible(true);
  ventana.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
              System.exit(0);
              
        }
  });
  ventana.setResizable(false);
  createBufferStrategy(2);
  strategy = getBufferStrategy();
  requestFocus();

So, I have to put somewhere the dispose() and setVisible(false) somewhere. Or do I have to add a windowListener to the class that extends JApplet? The code above is in another class called FrontEnd, which just has its main/init() method called from a class that extends JApplet. When I ran it, I ended up having the small applet window pop up, then the JFrame window pop up. Realistically, I want the JFrame in the original webpage, but I just want the window to close for now :wink:

Any help at all would be appreciated.
Jay

Oh, haha. I just figured it out. ;D
Closing the original window kills the applet and JFrame. Very sloppy, but it’ll do for now.

In case you don’t know, you don’t even need a WindowListener for your JFrame in this case. Look at the Javadoc and you’ll see this method for JFrame:

So you could do:


ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and the whole application will close when the ‘X’ button (if you’re on windows) is pressed.

Since it can be applet, it might be a better idea to use DISPOSE_ON_CLOSE instead. Either way there is no need for the WindowListener

Hmm, it didn’t start out as an applet, but thanks, that’s all I needed.