Components: Button and TextField
Two of the most common components you are likely to work with are buttons
and textfields. This applet provides a good examples of how to work with
them.
------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class peaks3 extends Applet{
TextField input, output;
String user="";
Button trigger;
public void init(){
setBackground(Color.yellow);
input = new TextField("input");
output = new TextField(" ---- output ----- ");
trigger = new Button("Process");
input.setBackground(Color.white);
output.setBackground(Color.white);
add(input); add(trigger); add(output);
trigger.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent k) {
user = input.getText();
if(user.equals("California")) output.setText("Mt. Whitney");
else if(user.equals("Nevada") ) output.setText("Boundary Peak");
else if(user.equals("Arizona") ) output.setText("Humphrey's Peak");
else if(user.equals("South Dakota") ) output.setText("Harney Peak");
else if(user.equals("Colorado") ) output.setText("Mt. Elbert");
else if(user.equals("Oregon") ) output.setText("Mt. Hood");
else if(user.equals("Washington") ) output.setText("Mt. Ranier");
else if(user.equals("Idaho") ) output.setText("Borah Peak");
else if(user.equals("Montana") ) output.setText("Granite Peak");
else if(user.equals("Utah") ) output.setText("King's Peak");
else if(user.equals("Wyoming") ) output.setText("Gannett Peak");
else if(user.equals("New Mexico") ) output.setText("Wheeler Peak");
else output.setText("State Not Recognized");
}
});
}
}
//
------------------------------------------------------------
------------------------------------------------------------
Assignment:
You will create a real simple calculator. It will have two input textfields
for numbers and four buttons labeled ADD, SUBTRACT, MULTIPLY, and DIVIDE.
Your calculator will have one output textfield for the answer. You may need
to review the lesson on numeric input. Your applet only needs to be able to
handle integers. If the person enters as input symbols which your applet
doesn't recognize as integers and error message should be displayed in the
output textfield.