Solved

Why won't this application close?

Posted on 2003-11-15
7
260 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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 …
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:

910 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

22 Experts available now in Live!

Get 1:1 Help Now