home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
WDESAMPL.BIN
/
degrees.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
4KB
|
147 lines
import java.awt.*;
public class degrees extends java.applet.Applet {
static boolean FSelected;
public void init() {
//{{INIT_CONTROLS
setLayout(new GridLayout(2,2,0,0));
setSize(265,61);
edit1 = new java.awt.TextField(15);
edit1.setBounds(0,0,265,30);
add(edit1);
group1 = new CheckboxGroup();
Fahrenheit = new java.awt.Checkbox("Fahrenheit", group1, true);
Fahrenheit.setBounds(132,0,265,30);
add(Fahrenheit);
clearbutton = new java.awt.Button();
clearbutton.setActionCommand("button");
clearbutton.setLabel("Clear");
clearbutton.setBounds(0,30,132,30);
clearbutton.setBackground(new Color(8421504));
add(clearbutton);
Celcius = new java.awt.Checkbox("Celcius", group1, false);
Celcius.setBounds(132,30,132,30);
add(Celcius);
//}}
FSelected = true;
edit1.requestFocus();
//{{REGISTER_LISTENERS
SymItem lSymItem = new SymItem();
Celcius.addItemListener(lSymItem);
Fahrenheit.addItemListener(lSymItem);
SymAction lSymAction = new SymAction();
clearbutton.addActionListener(lSymAction);
//}}
validate();
}
//{{DECLARE_CONTROLS
java.awt.TextField edit1;
java.awt.Checkbox Fahrenheit;
CheckboxGroup group1;
java.awt.Button clearbutton;
java.awt.Checkbox Celcius;
//}}
//-------------------------------------------------------------
// This file has been migrated from the 1.0 to 1.1 event model.
// This method is not used with the new 1.1 event model. You can
// move any code you need to keep, then remove this method.
//-------------------------------------------------------------
//
//
// public boolean handleEvent(Event event) {
// if (event.id == Event.ACTION_EVENT && event.target == Celcius) {
// clickedCelcius();
// return true;
// }
// else
// if (event.id == Event.ACTION_EVENT && event.target == Fahrenheit) {
// clickedFahrenheit();
// return true;
// }
//
// return super.handleEvent(event);
// }
//-------------------------------------------------------------
void clickedFahrenheit(java.awt.event.ItemEvent event) {
float degree;
if(FSelected == true) return;
try {
degree = (Float.valueOf(edit1.getText())).floatValue();
} catch(NumberFormatException e) {FSelected = true; return; }
if( degree <= 0 )
edit1.setBackground(Color.cyan);
else if( degree >= 100 )
edit1.setBackground(Color.red);
else edit1.setBackground(Color.white);
edit1.setText(String.valueOf(1.8*degree+32));
FSelected = true;
}
void clickedCelcius(java.awt.event.ItemEvent event) {
float degree;
if(FSelected == false) return;
try {
degree = (Float.valueOf(edit1.getText())).floatValue();
} catch (NumberFormatException e) {FSelected = false; return; }
if( degree <= 32 )
edit1.setBackground(Color.cyan);
else if( degree >= 212 )
edit1.setBackground(Color.red);
else edit1.setBackground(Color.white);
edit1.setText(String.valueOf((degree-32.0)/1.8));
FSelected = false;
}
class SymItem implements java.awt.event.ItemListener
{
public void itemStateChanged(java.awt.event.ItemEvent event)
{
Object object = event.getSource();
if (object == Celcius)
clickedCelcius(event);
else if (object == Fahrenheit)
clickedFahrenheit(event);
}
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == clearbutton)
clearbutton_ActionPerformed(event);
}
}
void clearbutton_ActionPerformed(java.awt.event.ActionEvent event)
{
// to do: code goes here.
//{{CONNECTION
// Set the text for TextField...
edit1.setText("");
edit1.setBackground(Color.white);
//}}
}
}