Solved

basic Java question

Posted on 2002-03-27
7
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 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