Solved

basic Java question

Posted on 2002-03-27
7
176 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now