Solved

Window quitter not working

Posted on 2000-03-08
9
331 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

18 Experts available now in Live!

Get 1:1 Help Now