CS 201 - Computer Science I - Spring 2003
Examples
Loyola College >
Department of Computer Science >
CS 201 >
Examples >
Using widgets
source code
import java.awt.*;
import java.applet.*;
/**
* Displays samples of six different kinds of
* widgets.
*
* @author Jim Glenn
* @version 0.1 3/10/2003
*/
public class Components extends Applet
{
/**
* Creates an object for each widget. The
* objects are created with new
* and made part of the applet with the
* add method from the
* Applet class. init
* is a method that is automatically executed
* once when the applet is first loaded.
*/
public void init()
{
// make six widgets; by default they
// are displayed across the top of the
// applet in the order thay are added
// Buttons allow the user to initiate actions
Button okButton; // declare name
okButton = new Button("OK"); // create object
this.add(okButton); // add to applet
Button bigButton;
bigButton = new Button("This button is really wide");
this.add(bigButton);
// Labels are used to let the user know
// what other widgets are for
Label nameLabel;
nameLabel = new Label("Name:");
this.add(nameLabel);
// TextFields allow users to type input;
// this one will be 10 characters wide
TextField nameInput;
nameInput = new TextField(10);
this.add(nameInput);
// Checkboxes allow users to select and
// unselect options
Checkbox robsBox;
robsBox = new Checkbox("YO");
this.add(robsBox);
// Lists allow users to choose from
// a list of items
List aList;
aList = new List();
aList.add("Choice 1"); // adds items to the list
aList.add("Choice 2");
this.add(aList);
// a Choice is like a List but is
// displayed differently
Choice aChoice;
aChoice = new Choice();
aChoice.add("CS 201");
aChoice.add("CS 202");
aChoice.add("CS 301");
this.add(aChoice);
}
}