Solved

basic Java question

Posted on 2002-03-27
7
204 Views
Last Modified: 2010-03-31
why when i'm click the "x" button on the frame it won't close?
whats wrong with this code?

import java.awt.*;
import java.awt.event.*;

public class TalkServerGUI extends WindowAdapter{
   public static void main(String args[]) {
      Frame f = new Frame();
      f.setTitle("Scott's Frame");
      f.setSize(200, 100);
      f.setVisible(true);
  }


public void windowClosing(WindowEvent we) {
                System.exit(0);
        }
}

i'm new in this area so an explanation with code would be greate.

0
Comment
Question by:sedgwick
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6901798
You haven't added an instance of TalkServerGUI as a listner for frames window event, so it never gets notified:

Try adding:

f.addWindowListener(new TalkServerGUI());
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6901832
Use this modified code, like objects has suggested :
import java.awt.*;
import java.awt.event.*;

public class TalkServerGUI extends WindowAdapter{
   public static void main(String args[]) {
      Frame f = new Frame();
      f.setTitle("Scott's Frame");
      f.addWindowListener(new TalkServerGUI());
      f.setSize(200, 100);
      f.setVisible(true);
  }


public void windowClosing(WindowEvent we) {
                System.exit(0);
        }
}

0
 

Expert Comment

by:0xDEADBEEF
ID: 6901970
Hmmm... I guess this might work, but WindowAdapter is an event-handling class, so it doesn't actually make sense to make it the main class. In case you want to do something w/ ur frame later on, this might get tough...

I'd rather try something like this:

public class ScottsFrame
  extends Frame {

  public ScottsFrame(){
    super("Scott's Frame"); // set title
    setSize(300, 200);

    //This will cause the window to close when the
    //close button is clicked.

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e){
          System.exit(0);
        }
      });

    //do some more stuff to ur Frame, adding controls and stuff

    setVisible(true);
  }

  public static void main(String[] args){
    ScottsFrame myFrame = new ScottsFrame();
  }
}

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:msterjev
ID: 6902075
The most elegant solution is the simplest. Just use javax.swing.JFrame and type this:

import javax.swing.*;

public class MyFrame extends JFrame
{
      public MyFrame()
      {
         super();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         . . . . . . . . . .
      }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 6903933
I don't see a problem with adding an event handler to the main GUI class, in fact I see it as preferrably considering the event action affects the entire GUI.
Subclassing Frame has it's limitations as it results in a frame class that exits the app whenever any instance is closed. If you had multiple frames this may not be preferable (though it might).
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7923789
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:modulo
ID: 7978420
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete Android all or certain applications data programmatically 9 111
jboss 7.1 start up error 1 61
java example issue 5 43
junit as external jar or library 7 38
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question