Overview of the Java UI |
The applet on this page shows you the graphical UI (GUI) components the AWT provides. With the exception of menus, every GUI component is implemented with a subclass of the AWT Component class.
Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go toexample-1dot1/GUIWindow.html
.Implementation Note: The applet is implemented as a button that brings up the window showing the components. The window is necessary because the program includes a menu, and menus can be used only in windows. The program has a
main()
method so it can run as an application. TheAppletButton
class provides an applet framework for the window. AppletButton is a highly configurable applet that's discussed on the following pages: Deciding Which Parameters to Support and Writing the Code to Support Parameters.
The Basic Controls: Buttons, Checkboxes, Choices, Lists, Menus, and Text Fields
The Button, Checkbox, Choice, List, MenuItem, and TextField classes provide basic controls. These are the most common ways that users give instructions to Java programs. When a user activates one of these controls -- by clicking a button or by pressing Return in a text field, for example -- it posts an event (ACTION_EVENT). An object that contains the control can react to the event by implementing theaction()
method.Other Ways of Getting User Input: Sliders, Scrollbars, and Text Areas
When the basic controls aren't appropriate, you can use the Scrollbar and TextArea classes to get user input. The Scrollbar class is used for both slider and scrollbar functionality. You can see an example of sliders in The Anatomy of a GUI-Based Program. Scrollbars are automatically included in lists and text areas (as shown in the example program) and in ScrollPane objects.The TextArea class simply provides an area to display or allow editing of several lines of text. As you can see from the applet on this page, text areas automatically include scrollbars.
Creating Custom Components: Canvases
The Canvas class lets you write custom Components. With your Canvas subclass, you can draw custom graphics to the screen -- in a paint program, image processor, or game, for example -- and implement any kind of event handling.Labels
A Label simply displays an unselectable line of text.Containers: Windows, Panels, and Scroll Panes
The AWT provides three types of containers, all implemented as subclasses of the Container class (which is a Component subclass). The Window subclasses -- Dialog, FileDialog, and Frame -- provide windows to contain components. A Panel groups components within an area of an existing window. A ScrollPane is similar to a panel, but its purpose is more specialized: to display a potentially large component in a limited amount of space, generally using scrollbars to control which part of the component is displayed.Frames create normal, full-fledged windows, as opposed to the windows that Dialogs create, which are dependent on Frames and can be modal. When you select the "File dialog..." item in the menu, the program creates a FileDialog object, which is a Dialog that can be either an Open or a Save dialog.
Here is a picture of the FileDialog window that the Solaris Applet Viewer brings up:
The example program at the beginning of this section uses a Panel to group the label and the text area, another Panel to group them with a canvas, and a third Panel to group the text field, button, checkbox, and pop-up list of choices. All these Panels are grouped by a Frame object, which presents the window they're displayed in. The Frame also holds a menu and a list.
Summary
This page presented a whirlwind tour of the AWT components. Every component this page mentions is described in detail in Using Components, the GUI Building Blocks.
Overview of the Java UI |