Solved

basic Java question

Posted on 2002-03-27
7
212 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

695 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