?
Solved

basic Java question

Posted on 2002-03-27
7
Medium Priority
?
215 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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