Simple Widgets:
A Java AWT encapsulates several of the controls general to most GUIs. Specially, these are the Button, Choice, Checkbox, Label, List, and Scrollbar classes. Figure displays an applet which shows the AWT simple widgets.
The given code displays the Simple applet that contains the simple AWT widgets. The applet's init () method creates an instance of every simple widget and adds it to the applet.
import java.awt.*;
public class Simple extends java.applet.Applet {
public void init() {
Button button = new Button( "Quit" ) ;
Checkbox checkbox = new Checkbox( "Test" ) ;
The Choice and List objects both involve addItem() methods. These methods permit you to fill the control along with the items you specify. Unlike a few GUIs, the AWT Choice and List controls do not sort the items they hold. They are show in the series in that you add them.
Choice choice = new Choice () ;
// fill the Choice choice.addItem( "Clinton" ) ;
choice.addItem( "Dole" ) ;
choice.addItem( "Perot" ) ;
choice.addItem( "Browne" ) ;
choice.addItem( "Nader" ) ;
Label label = new Label( " label" );
List list = new List( 5, false ) ;
// fill the List list.addItem( "Clinton" ) ;
list.addItem( "Dole" ) ;
list.addItem( "Perot" ) ;
list.addItem( "Browne" ) ;
list.addItem( "Nader" ) ;
Scrollbar scrollbar = new Scrollbar (scrollbar.HORIZONTAL);
For show these controls, you must add them to the applet's layout using the add () method:
// add the controls
add( button ) ;
add( checkbox ) ;
add( choice ) ;
add( label ) ;
add( list ) ;
add( scrollbar ) ;
}
}
The above applet displays the easy widgets. To be truly meaningful, an applet should do more than only display the controls-the applet must also be interactive. That brings up the topic of event handling. A DemoFrame applet presented later in this lesson demonstrates event handling.
At the top of the AWT Hierarchy is the Component class. A Component is abstract classes which encapsulate all of the attributes of a visual component. All user interface elements which are shown on the screen and that interact along with the user are subclass of Component. It declares over a hundred public methods which are responsible for maintaining events, like as mouse and keyboard input, positioning and repainting and sizing the window.
The Component object is responsible for remembering the present foreground and background colors and the presently selected text font.
Controls are component which permits a user to interact along with your application in several ways.