// A Java applet that converts Fahrenheit temperatures to Celsius temperatures.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import appletIO.AppletIO;
public class TempConversion extends Applet
{
private Button Calculate;
private Label lblFahrenheit, lblCelsius;
private TextField txtFahrenheit, txtCelsius;
private Panel p;
public void init ()
{
setBackground (Color.cyan);
p = new Panel ();
p.setLayout (new GridLayout (5, 1, 12, 12));
txtFahrenheit = setField (p, txtFahrenheit, lblFahrenheit, "Fahrenheit");
txtCelsius = setField (p, txtCelsius, lblCelsius, "Celsius");
Calculate = new Button ("Calculate");
Calculate.addActionListener (new CalculateListener ());
p.add (Calculate);
add (p);
} // method init
// Method to create a TextField and a Label and add them to the Panel.
private TextField setField (Panel p, TextField txt, Label lbl, String label)
{
txt = new TextField (10);
lbl = new Label (label);
p.add (lbl);
p.add (txt);
return txt;
} // method setField
// Inner class to listen for the Calculate button.
class CalculateListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double fahrenheit, celsius;
fahrenheit = AppletIO.getDouble (txtFahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
txtCelsius.setText ("" + AppletIO.decimals (celsius));
} // method actionPerformed
} // class CalculateListener
} // class TempConversion
Dostları ilə paylaş: |