Solved

Why won't this application close?

Posted on 2003-11-15
7
283 Views
Last Modified: 2010-03-31
Hi All,

This error may be the result of poor construction but when I click the title
 bar box X to close this window will not close?

And when I try uncommenting the processWindowEvent at the
bottom does not seem to work either. Infact it is even worse. I have to go to
 the Win2k task Mgr (ctrl, alt, delete)  to shut it off

Help appreciated.

Delphi3



package systeminouttotxtfieldinout;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class framedTextFieldInOut {
  public framedTextFieldInOut() {
    boolean packFrame = false;
    //Construct the application
    Frame1 frame = new Frame1();
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation( (screenSize.width - frameSize.width) / 2,
                      (screenSize.height - frameSize.height) / 2);
    frame.setTitle("The Conversions of Fahrenheit and Celsius ");
    frame.setVisible(true);

  }

  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new framedTextFieldInOut();
  }

  public class Frame1
      extends Frame {
    //Fahrenheit,Celsius
    float Fahr, Cels;
    Label f = new Label("Input F  Press Return Conversion below ");
    Label c = new Label("Input C Press Return Conversion above ");
    TextField tf1 = new TextField(5);
    TextField tf2 = new TextField(5);

    Frame1() {
      setSize(400, 100);
      setLayout(new FlowLayout());
      add(f);
      tf1.addActionListener(new Fahren());
      add(tf1);
      add(c);
      tf2.addActionListener(new Celsi());
      add(tf2);

    }

    class Fahren
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Fahr = Float.valueOf(tf1.getText()).floatValue();
        Cels = (float) ( (Fahr - 32) / 1.8);
        tf2.setText(Float.toString(Cels));
      }
    }

    class Celsi
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Cels = Float.valueOf(tf2.getText()).floatValue();
        Fahr = (float) (1.8 * Cels) + 32;
        tf1.setText(Float.toString(Fahr));
      }
    }

    //Overridden so we can exit when window is closed
    // protected void processWindowEvent(WindowEvent e) {
    //   super.processWindowEvent(e);
    //   if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    //     System.exit(0);

    //   }
    //  }

  }
}
0
Comment
Question by:delphi3
[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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9754597
The method

   protected void processWindowEvent(WindowEvent e)

should be

   public void processWindowEvent(WindowEvent e)

Try that and see how it works out.

\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754905
>>UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Is not appropriate in a non-Swing application and is anyway redundant
0
 
LVL 4

Author Comment

by:delphi3
ID: 9755954
Hi
Sorry but I tried both suggestions and neither of them make a difference.

So any other bright ideas?

Thanks Delphi3
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 9755991
Make this the first line of your frame constructor:

addWindowListener(this);
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9756940
Add following to your ctor:

enableEvents(AWTEvent.WINDOW_EVENT_MASK);
0
 
LVL 4

Author Comment

by:delphi3
ID: 9768305
Hi All,
Considering the suggestions that were offered objects' solution offers the one that allows the window to close
when I placed his line in the section of the frame and then  uncommented the lines previously written in the program.

....... copied from above with changes in place .........

   public class Frame1
      extends Frame {
    //Fahrenheit,Celsius
    float Fahr, Cels;
    Label f = new Label("Input F  Press Return Conversion below ");
    Label c = new Label("Input C Press Return Conversion above ");
    TextField tf1 = new TextField(5);
    TextField tf2 = new TextField(5);

    Frame1() {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);// added from EE objects
      setSize(400, 100);
      setLayout(new FlowLayout());
      add(f);
      tf1.addActionListener(new Fahren());
      add(tf1);
      add(c);
      tf2.addActionListener(new Celsi());
      add(tf2);

    }

    class Fahren
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Fahr = Float.valueOf(tf1.getText()).floatValue();
        Cels = (float) ( (Fahr - 32) / 1.8);
        tf2.setText(Float.toString(Cels));
      }
    }

    class Celsi
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Cels = Float.valueOf(tf2.getText()).floatValue();
        Fahr = (float) (1.8 * Cels) + 32;
        tf1.setText(Float.toString(Fahr));
      }
    }

    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        System.exit(0);

      }
    }

  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9768334
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

756 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