Solved

Window quitter not working

Posted on 2000-03-08
9
338 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
Way to decrease size of apk file 9 66
Java exception bubble up 2 18
How to convert from xls to xlsx using java 7 39
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 …
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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