rockcairn logoRockCairn.com rockcairn logo
/************************************************************
* Aaron Penner
* Homework1
* Due 1/25/2000 11:59PM
*
* Java Applet
* Input: two numbers, one textfield, process button
* Output: text area out put of the greater of two numbers and
* the text field input in Caps.
**************************************************************/
import java.io.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GuiInputSwingTest extends java.applet.Applet
{ public void init()
{
this.setLayout(new BorderLayout());

Panel panel = new Panel();

/* Text field for the first number */
panel.add(new Label("Enter First Number: "));
num1_tf = new TextField(10);
panel.add(num1_tf, "Center");

/* Text field for the second nubmer */
panel.add(new Label("Enter Seacond Number: "));
num2_tf = new TextField(10);
panel.add(num2_tf, "Center");

/* Text field for name input */
panel.add(new Label("Enter Your Name: "));
name_tf = new TextField(20);
panel.add(name_tf, "Center");

/* Button for input processing and button press response actions */
processInput_bt = new Button("Process the Inputs");
panel.add(processInput_bt, "Center");
processInput_bt.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{
double num1,num2;
/* get the input from the text first field and process as a double */
try{
String n1 = num1_tf.getText();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(n1);
num1 = n.doubleValue();
}
catch(ParseException e)
{ num1 = 0;
}
/* get the input from the text second field and process as a double */
try{
String n2 = num2_tf.getText();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(n2);
num2 = n.doubleValue();
}
catch(ParseException e)
{ num2 = 0;
}

/* get the input for the name text field and convert to upper case */
String s = name_tf.getText();
String yourname = s.toUpperCase();

if (num1 > num2)
outputArea_ta.setText("The Bigger of the numbers is: " + num1 + "\n");
else
outputArea_ta.setText("The Bigger of the numbers is: " + num2 + "\n");

outputArea_ta.append("Your name in caps is: " + yourname);

}
});

/* create the output test area */
outputArea_ta = new TextArea(5,40);
panel.add(outputArea_ta, "Center");

/* add the panel to the main applet container for display */
this.add(panel, "Center");

}
private TextField num1_tf, num2_tf, name_tf;
private TextArea outputArea_ta;
private Button processInput_bt;

} /* end of class GuiInputTest */