Solved

Window quitter not working

Posted on 2000-03-08
9
334 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
 
LVL 7

Expert Comment

by:conick
ID: 2596622
whoops sorry about the double post... first time ever :(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 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…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now