Solved

Window quitter not working

Posted on 2000-03-08
9
340 Views
Last Modified: 2008-03-06
I have a program that seems to be working fine, while working with the awt, but the windowquitter will not work....Here is the code:import java.awt.*;
import java.awt.event.*;

      
public class March2exer2 extends Frame
{
      
      public static void main(String[] args)
      {
             Frame aWindow = new Frame("Show FlowLayout");
            //myFrame frm = new myFrame();
                  Toolkit theKit = aWindow.getToolkit();
            Dimension wndSize = theKit.getScreenSize();
            aWindow.setBounds(wndSize.width/4, wndSize.height/4,
                                       wndSize.width/2, wndSize.height/2);
            
            
                  aWindow.setSize(400, 200);
                  
                  aWindow.setBackground(Color.pink);
                  FlowLayout flow = new FlowLayout();
                  aWindow.setLayout(flow);
                  for(int i = 1; i <= 10; i++)
                        aWindow.add(new Button("Component " + i));
                  aWindow.show();
                  
}

      public March2exer2()

      {
            super();
            WindowQuitter wquit = new WindowQuitter();
            addWindowListener(wquit);
            //setSize(800, 400);
            //setTitle("Show FlowLayout");
      }
      class WindowQuitter extends WindowAdapter
      {
            
            public void WindowClosing(WindowEvent e)
            {
            System.exit(0);
            }
      }
}

any ideas would be appreciated.

0
Comment
Question by:deedeeh
  • 5
  • 3
9 Comments
 
LVL 5

Expert Comment

by:mbormann
ID: 2596486
I think spelling is problem
>>>public void WindowClosing(WindowEvent e)

instead of
addWindowListener(wquit);

use
addWindowListener(new WindowQuitter());

or

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

0
 
LVL 7

Expert Comment

by:conick
ID: 2596594
To add:
Notice the small w instead of the W.
Adapter classes need to be exactly correct (case and spelling) to override the adapter methods.
Adapter classes are a convience so that you dont have to implement the WindowListener interface and override each method.  The downside is that if you mispell something there is no compiler error because it thinks that you are adding a method instead of overriding the interface method. (ie WindowClosing() is different method than windowClosing())

The following events:
1)
addWindowListener(new WindowQuitter());
....
class WindowQuitter extends WindowAdapter  
2)
WindowAdapter wquit= new WindowAdapter();
addWindowListener(wquit);
....
class WindowQuitter extends WindowAdapter
3)
addWindowListener(new WindowAdapter()  {
  public void windowClosing() {

are functionally equivalent

How you call the adapter with addWindowListener is up to you, although I normally use mbormanns method (2) also or the inner class (3). But its a personal preference.
0
 
LVL 7

Accepted Solution

by:
conick earned 100 total points
ID: 2596618
To add:
Notice the small w instead of the W.
Adapter classes need to be exactly correct (case and spelling) to override the adapter methods.
Adapter classes are a convience so that you dont have to implement the WindowListener interface and override each method.  The downside is that if you mispell something there is no compiler error because it thinks that you are adding a method instead of overriding the interface method. (ie WindowClosing() is different method than windowClosing())

The following events:
1)
addWindowListener(new WindowQuitter());
....
class WindowQuitter extends WindowAdapter  
2)
WindowAdapter wquit= new WindowAdapter();
addWindowListener(wquit);
....
class WindowQuitter extends WindowAdapter
3)
addWindowListener(new WindowAdapter()  {
  public void windowClosing() {

are functionally equivalent

How you call the adapter with addWindowListener is up to you, although I normally use mbormanns method (2) also or the inner class (3). But its a personal preference.


ack.
Also you never call your constructor from the main method.
Instead of using:
March2exer2 frame= new March2exer2("Show FlowLayout");
you use
Frame aWindow = new Frame("Show FlowLayout");

Change this for any of the window closing code to be effective.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 7

Expert Comment

by:conick
ID: 2596622
whoops sorry about the double post... first time ever :(
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2599150
>>>first time ever
is it so ?
:)
0
 
LVL 7

Expert Comment

by:conick
ID: 2601405
Im always thinking "why do people post a question twice?"
and then I go and do it. *blush*

deedeeh:
One more thing about the code.
You use setBounds() which determines the windows position and size.
You then call setSize() which sets the size again.  Only setBounds is needed.  (or setSize() coupled with setPosition()).
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2602710
As conick said that his was a double posting, maybe deedeeh didn't see that the answer to his/her problem probably was in the last lines of the second posting and not on the firs one.

Here is what conick said:

> Also you never call your constructor from the main method.
> Instead of using:
> March2exer2 frame= new March2exer2("Show FlowLayout");
> you use
> Frame aWindow = new Frame("Show FlowLayout");

> Change this for any of the window closing code to be effective.
0
 
LVL 7

Expert Comment

by:conick
ID: 2605060
I really screwed that up.
I didnt realize it was on one and not the other. Thanks AlexJ.
Actuall both problems need to be addressed before the code will work.

from mbormann:
WindowClosing() needs to be windowClosing()

and:
Frame aWindow = new Frame("Show FlowLayout");
needs to be:
March2exer2 frame= new March2exer2("Show FlowLayout");
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2607420
deedeeh,
hope it's solved now?
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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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