Stuck on how to get a value to output through a JLabel
843785Jul 12 2008 — edited Jul 12 2008Hi,
I'm a novice in Java. The code below converters Fahrenheit to Celsius and Kelvin degrees. However, I'm stuck and don't know how the convertion to output on a JLabel. Any tips please? Thank you.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
public class TempConverter extends JFrame
{
private int fahrenheit;
private JTextField enterFahrenheit; // input Fahrenheit texfield
private JLabel jLabelTitle;//
private JLabel jLabelIntro;//
private JLabel jLabelCelsius; // converted Celsius display label
private JLabel jLabelKelvin; // converted Kelvin display label
private JButton jButton1Clear; // clears user input at texfield
private Color background; // background color of application
// set up GUI and initialize values
public TempConverter ()
{
/* constructor line of code that calls the superclass constructor and sets the
title of this application to "Fausto's Temperature Converter" */
super("Fausto's Temperature Converter");
setLayout (new FlowLayout()); //set new frame layout and enables the display components
background = Color.LIGHT_GRAY; // set background to light gray
String string = "";
jLabelTitle = new JLabel ("Fausto's Temperature Converter"); // main frame title
jLabelTitle.setFont(new java.awt.Font("Tahoma", 1, 20)); //font style, emphasis, size
add(jLabelTitle); //adds GUI component
jLabelIntro = new JLabel ("Please enter Fahrenheit degrees: "); // main frame title
jLabelIntro.setFont(new java.awt.Font("Tahoma", 1, 20)); //font style, emphasis, size
add(jLabelIntro); //adds GUI component
enterFahrenheit= new JTextField(5); // to enter guesses
enterFahrenheit.setSelectedTextColor(new java.awt.Color(51, 51, 255)); // RGB values for text color
enterFahrenheit.setEditable(true); // enables to type text in JTextField
enterFahrenheit.addActionListener( new DegreeHandler( ) ); // calls inner class when user presses the enter key
enterFahrenheit.setBackground(Color.YELLOW); // set JTextField background to yellow
add(enterFahrenheit); //adds GUI component
jLabelCelsius = new JLabel(); // describe the game
jLabelCelsius.setFont(new java.awt.Font("Tahoma", 0, 18)); //font style, emphasis, size
string = String.format("Equivalent Celsius is: ");
jLabelCelsius.setText(string);
add(jLabelCelsius); //adds GUI component
jLabelKelvin = new JLabel( "Kelvin equivalent is: "); // prompt user to enter a number
jLabelKelvin.setFont(new java.awt.Font("Tahoma", 0, 18)); //font style, emphasis, size
add(jLabelKelvin); //adds GUI component
}
// react to new Fahrenheit
public void converter(int fahrenheit)
{
double celsius = 5/9 + (fahrenheit - 32);
System.out.println("Equivalent to " + celsius + " C");
double kelvin = celsius + 273.15;
System.out.println("Equivalent to " + kelvin + " K");
}
// inner class acts on user input
class DegreeHandler implements ActionListener
{
int fahrenheit = 0;
public void actionPerformed( ActionEvent e )
{
/* code that will obtain the enterFahrenheit, convert it to an int and
pass that value to the converter method */
String string = "";
if (e.getSource() == enterFahrenheit);
string = string.format ("%s", e.getActionCommand());
System.out.println ("User entered " + string);
converter (fahrenheit = Integer.parseInt (string));
} // end method actionPerformed
} // end inner class DegreeHandler
}//end of TempConverter class