5. 5
To build a GUI...
• Make somewhere to display things—usually a
Frame or Dialog (for an application), or an Applet
• Create some Components, such as buttons, text
areas, panels, etc.
• Add your Components to your display area
• Arrange, or lay out, your Components
• Attach Listeners to your Components
– Interacting with a Component causes an Event to occur
– A Listener gets a message when an interesting event
occurs, and executes some code to deal with it
10. 10
Some types of components
Label Button
Button
Checkbox
Choice
List
Scrollbar
TextField TextArea
CheckboxGroup
Checkbox
11. 11
Creating a Graphical User Interface
• GUI programming in Java is based on three concepts:
– Components. A component is an object that the user can see on
the screen and—in most cases—interact with.
– Containers. A container is a component that can hold other
components.
– Events. An event is an action triggered by the user, such as a key
press or mouse click.
• Designing a graphical user interface involves creating
components, putting them into containers, and arranging
for the program to respond to events.
12. COMPONENT:
Component class is at the top of the AWT hierarchy. Its an abstract class that encapsulates all
of the attributes of a visual component . All the user interface elements that are displayed on
the screen and that interact with the user are subclasses of Component.
CONTAINER:
The Container class is a subclass of Component. It has additional methods that allow other
Component objects to be nested within it. other container objects can be stored inside a
Container. Various layouts can be used by implementing Layout Manager.
PANEL:
It’s a subclass of Container . It does not have any new methods .It implements Container .
Panel is the super class for Applet . The output on the screen directed to an Applet is actually
drawn on the surface of Panel object .A panel is a window that does not has a title bar ,Menu
bar or a border.
Components can be added to the Panel using add() method inherited from Container. Once
the components are added they can be resized , positioned using setSize()
,setLocation(),setBounds() etc.
WINDOW: A Window class creates a top-level window.Window is not contained within any
other object. It is placed directly on the desktop.
13. FRAME:
It’s a subclass of Window and has a title bar, menu bar and resizing corners also. When a
Frame object is created a warning msg such as “Java Applet Window” will be displayed to
ensure the user that the Frame is created by the Applet Window not by any other running
software.
CANVAS:
It is not the hierarchy of Frame of applet. But it is another type of Window used in GUI.
14. Working with Frame Windows
Frames are most widely used after the usage of applets. The Constructors use are
The first form creates the window without ant title. The second form creates the window with a
title . Windows can be resized , made to visible and closed also using the following methods.
15. Closing the windows can be done by either using setVisible(false) or using windowClosing()
method of windowListener interface.
Hinweis der Redaktion
This slide shows the hierarchy of classes which will be covered in this chapter. If you check the Java API documentation, you will note that there are many more classes in the java.awt package.