Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Window quitter not working

Posted on 2000-03-08
9
Medium Priority
?
345 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
[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
  • 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 400 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

715 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