Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 1721 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));
     ventana.setBounds(0, 0, Stage.WIDTH, Stage.HEIGHT);
     ventana.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
     strategy = getBufferStrategy();

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.
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:


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:

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  

EgonOlsen (60 views)
2018-06-10 19:43:48

EgonOlsen (42 views)
2018-06-10 19:43:44

EgonOlsen (61 views)
2018-06-10 19:43:20

DesertCoockie (241 views)
2018-05-13 18:23:11

nelsongames (142 views)
2018-04-24 18:15:36

nelsongames (141 views)
2018-04-24 18:14:32

ivj94 (883 views)
2018-03-24 14:47:39

ivj94 (144 views)
2018-03-24 14:46:31

ivj94 (795 views)
2018-03-24 14:43:53

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!