FahrenheitPanel - add (outputLabel) add (resultLabel)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 //******************************************************************** // FahrenheitPanel.java Author: Lewis/Loftus // // Demonstrates the use of text fields. //******************************************************************** import java.awt.* import java.awt.event.* import javax.swing.* public class FahrenheitPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel 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 ("---") fahrenheit = new JTextField (5) fahrenheit.addActionListener (new TempListener()) add (inputLabel) add (fahrenheit)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: add (outputLabel) add (resultLabel) setPreferredSize (new Dimension(300, 75)) setBackground (Color.yellow) } //***************************************************************** // 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) Sheet1 Page 2 celsiusTemp = (fahrenheitTemp-32) * 5/9 resultLabel.setText (Integer.toString (celsiusTemp)) } } }...
View Full Document

This note was uploaded on 06/11/2009 for the course CS 205214 taught by Professor Balasooriya/kouvetakis during the Spring '09 term at ASU.

Page1 / 2

FahrenheitPanel - add (outputLabel) add (resultLabel)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online