Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bit of help with JFrame window listener  (Read 872 times)
0 Members and 1 Guest are viewing this topic.
Offline FoolishFate

Junior Newbie




Java games rock!


« Posted 2005-01-29 16:59:27 »

Hey guys.
I'm new to the boards, and relatively new to Java compared to most  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
Offline FoolishFate

Junior Newbie




Java games rock!


« Reply #1 - Posted 2005-01-29 17:12:39 »

Oh, haha. I just figured it out.  Grin
Closing the original window kills the applet and JFrame. Very sloppy, but it'll do for now.
Offline woogley
« Reply #2 - Posted 2005-01-29 17:58:49 »

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:

Quote

void setDefaultCloseOperation(int operation)
         Sets the operation that will happen by default when the user initiates a "close" on this frame.


So you could do:
1  
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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline FoolishFate

Junior Newbie




Java games rock!


« Reply #3 - Posted 2005-01-29 18:01:39 »

Hmm, it didn't start out as an applet, but thanks, that's all I needed.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!