Temperature

Temperature - add(new JButton("F ->...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * File: Temperature.java * ---------------------- * This program converts back and forth between Fahrenheit * and Celsius temperatures */ import acm.gui.*; import acm.program.*; import acm.graphics.*; import acm.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Temperature extends Program { public void init() { setLayout(new TableLayout(2, 3)); fahrenField = new IntField(32); fahrenField.setActionCommand("F -> C"); fahrenField.addActionListener(this); celsiusField= new IntField(0); celsiusField.setActionCommand("C -> F"); celsiusField.addActionListener(this); add(new JLabel("Degrees Fahrenheit")); add(fahrenField);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: add(new JButton("F -> C")); add(new JLabel("Degrees Celsius")); add(celsiusField); add(new JButton("C -> F")); addActionListeners(); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("F -> C")) { int f = fahrenField.getValue(); int c = GMath.round((5.0 / 9.0) * (f - 32)); celsiusField.setValue(c); } else if (cmd.equals("C -> F")) { int c = celsiusField.getValue(); int f = GMath.round((9.0 / 5.0) * c + 32); fahrenField.setValue(f); } } /* Private instance variables */ private IntField fahrenField; private IntField celsiusField; }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Ask a homework question - tutors are online