Overview of the Java UI |
The applet on this page shows you the graphical UI components we provide. Every graphical UI component is implemented with a subclass of the AWT Component class.
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. Here, for the curious, is the source code for the window that displays the components. (The Using Components lesson will show and explain examples of using each component.) The program has both an Applet subclass and a
main()
method, so it can run either as an applet or as an application. The program uses the AppletButton class to provide an applet framework for the window.
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: Canvases and Text Areas
When the basic controls aren't appropriate, you can use the Canvas and TextArea classes to get user input. The TextArea class simply provides an area to display or allow editing of several lines of text. Create a subclass of the Canvas class if you need draw custom graphics to the screen -- in a paint program, image processor, or game, for example.Yet More Components: Scrollbars and Labels
The AWT provides two more handy components: scrollbars and labels. Text areas automatically have scrollbars, but you can use the Scrollbar class to make other kinds of areas scroll. Labels simply display an uneditable, unselectable line of text.Containers: Windows and Panels
The AWT provides two types of containers, both implemented as subclasses of the Container class (which is a Component subclass). The Window subclasses -- Dialog, FileDialog, and Frame -- provide windows to contain components. Panels group components within an area of an existing window.The example program 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 the Applet object, since the Applet class is a subclass of Panel.
The example program uses a Frame to hold the Menu and List. (Frames create normal, full-fledged windows, as opposed to the windows that Dialogs create, which are dependent on Frames and can be modal.) When the program is run as an application, then the
main()
method creates a Frame to hold the Applet. Finally, 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.Note: [FileDialog -- perhaps all dialogs? -- doesn't seem to work in Netscape.] Since the File dialog is modal, this browser window may behave strangely (not scrolling or repainting, for example) until you dismiss the dialog.
Here is a picture of the window that the FileDialog brings up:
Overview of the Java UI |