Solved

Why won't this application close?

Posted on 2003-11-15
7
246 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 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