Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why won't this application close?

Posted on 2003-11-15
7
Medium Priority
?
309 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

824 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