8 Replies - 12853 Views - Last Post: 13 December 2009 - 02:44 PM Rate Topic: -----

#1 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 04:15 PM

Im having a slight issue with my code. The question is:

Write a temp. conversion application that converts from farenhieght to celsius. The user interface of this application should use Java GIU components. The GUI should include the following components:
1. A title
2. A prompt for entering the farenheight temp.
3. A text field for entering the temp.
4. The result of the conversion
5. Comments about the temp. with the black foreground and changed background color depending on the temp.

I have everything working fine, except the background color is not changing. What am I doing wrong?

Here is my code.

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

public class FahrenheitPanel extends JPanel
{
   private JLabel inputLabel, outputLabel, resultLabel,commentLabel, comment;
   private JTextField fahrenheit;



   //  Constructor: Sets up the main GUI components.

   public FahrenheitPanel()
   {
	  inputLabel = new JLabel ("Enter Fahrenheit temperature:");
	  outputLabel = new JLabel ("Temperature in Celsius: ");
	  resultLabel = new JLabel ("---");
	  commentLabel = new JLabel ("Comment:");
	  comment = new JLabel("---");



	  fahrenheit = new JTextField (5);
	  fahrenheit.addActionListener (new TempListener());

	  add (inputLabel);
	  add (fahrenheit);
	  add (outputLabel);
	  add (resultLabel);
	  add (commentLabel);
	  add (comment);

	  setPreferredSize (new Dimension(300, 75));
	  setBackground (Color.WHITE);
   }


   //  Represents an action listener for the temperature input field.

   private class TempListener implements ActionListener
   {

	  //  Performs the conversion when the enter key is pressed in
	  //  the text field.

	  public void actionPerformed (ActionEvent event)
	  {
		 int fahrenheitTemp, celsiusTemp;

		 String text = fahrenheit.getText();

		 fahrenheitTemp = Integer.parseInt (text);
		 celsiusTemp = (fahrenheitTemp-32) * 5/9;

		 resultLabel.setText (Integer.toString (celsiusTemp));

 		if(celsiusTemp > 30)
		{
			comment.setForeground(Color.BLACK);
			comment.setBackground(Color.RED);
			comment.setText("It is Hot!");
		}
		else if(celsiusTemp >20 && celsiusTemp <=30 )
		{
			comment.setBackground(Color.ORANGE);
			comment.setText("It is warm.");
		}
		else if(celsiusTemp >10 && celsiusTemp <=20)
		{
			comment.setBackground(Color.YELLOW);
			comment.setText("It is cool.");
		}
		else if(celsiusTemp >0 && celsiusTemp <=10)
		{
			comment.setBackground(Color.WHITE);
			comment.setText("It is chilly.");
		}
		else if(celsiusTemp <=0)
		{
			comment.setBackground(Color.GRAY);
			comment.setText("It is cold!");
		}

}
   }

   public static void main (String[] args)
	  {
		 JFrame frame = new JFrame ("Fahrenheit to Celsius Converter");
		 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

		 FahrenheitPanel panel = new FahrenheitPanel();

		 frame.getContentPane().add(panel);
		 frame.pack();
		 frame.setVisible(true);
   }


}



import javax.swing.JFrame;

public class Fahrenheit
{

   //  Creates and displays the temperature converter GUI.

   public static void main (String[] args)
   {
	  JFrame frame = new JFrame ("Fahrenheit");
	  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	  FahrenheitPanel panel = new FahrenheitPanel();

	  frame.getContentPane().add(panel);
	  frame.pack();
	  frame.setVisible(true);
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: GUI Fahrenheit to celsius conversion

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 05:22 PM

In your constructor:
comment.setOpaque(true);



That makes it visible. By default JLabel's background color is transparent
Was This Post Helpful? 0
  • +
  • -

#3 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 05:24 PM

awesome, thank you!
Was This Post Helpful? 0
  • +
  • -

#4 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 05:35 PM

If i wanted to use a try/catch block to prevent wrong input how would I go about doing that?
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 05:43 PM

If you mean if the user enters a non-integer?
try {
	fahrenheitTemp = Integer.parseInt (text);
} catch (NumberFormatException nfe) {
	// Error code
}

// Continue with program



Here is a warning though... You may consider using the Double.parseDouble() instead. That way if the user enters "12.0", then the program won't accept it.
Was This Post Helpful? 0
  • +
  • -

#6 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 09:40 PM

One last question. How come the comment line prints next to the celsius conversion instead of printing underneath it? So it would look like:

Enter Farenheight temperature:
Temperature in Celsius
Comment line
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 09:46 PM

Right now, you are using flow layout, which just shoves as many things right next to each other as possible. Now would be a good time to use GridLayout.

Add this to your constructor:
setLayout(new GridLayout(3,2));



That code sets up a new GridLayout manager so that it has 3 rows and 2 columns. Give it a whirl!

All right edited my post to show the code.

This post has been edited by Dogstopper: 09 December 2009 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

Re: GUI Fahrenheit to celsius conversion

Posted 09 December 2009 - 09:48 PM

Works perfect. Thank you!
Was This Post Helpful? 0
  • +
  • -

#9 ms710  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-November 09

Re: GUI Fahrenheit to celsius conversion

Posted 13 December 2009 - 02:44 PM

ok so I asked my professor if we had to use the try/catch block and she said that since she forgot to write it in the project requirements that it was not necessary for us to do so, but I would still like to know how to do it, except I'm having problems getting it to work. Could anyone explain how this must be done?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1