Download presentation
Presentation is loading. Please wait.
1
Aum Amriteshwaryai Namah
Unit 5 GUI
2
Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface (GUI) applications. Event Driven Programming Java API for GUI – java.awt and javax.swing.
3
Java APIs for GUI There are two sets of Java APIs for GUI programming:
AWT (Abstract Windowing Toolkit) and Swing. AWT APIs were introduced in JDK 1.0. Swing APIs, a much more comprehensive set of graphics libraries that enhances the AWT counterparts, has been integrated into core Java since JDK 1.2. These provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications.
4
Sample GUI using AWT
5
Container vs. Component
6
Container vs. Component
Two groups of classes are involved in a GUI program: Component classes: Components are elementary UI entities such as buttons, labels, text fields, and check boxes. Container classes: Containers, such as frames and panels, are used to hold UI components. A container can also hold containers.
7
Container vs. Component
It is important to note that in a GUI program: a component must be kept in a container. You need to identify the container and component. Every container has a method called add(Component c). E.g.:- A container (says aContainer) can invoke aContainer.add(aComponent) to add aComponent into itself.
8
Frame & Panel Frame is the top-level container of an AWT GUI program.
Frame has a title bar (containing program icon, title, minimize, maximize/restore-down and close buttons), an optional menu bar and program display area. Panel is an invisible rectangular box used to group related UI component. I
9
AWT Container Classes A GUI program must begin with a top-level container. The commonly-used top-level containers in AWT are Frame, Dialog and Applet. An AWT Frame is a "window”
10
AWT Container Classes An AWT GUI program is usually written as a subclass of java.awt.Frame class. An AWT Dialog is a pop-up window used for interacting with the users. A Panel is an invisible retangular box under a higher-level container, used to group and layout a set of related UI components
11
Hierarchy of the AWT Container classes
12
Component classes AWT provides many ready-made and reusable UI components. The frequently-used are: Button, TextField, Label, Checkbox, CheckboxGroup (radio buttons), List, and Choice
13
Component creation Three steps are necessary to create and place a UI component: Declare the component with an identifier; Construct via new operator and invoke the chosen constructor; Identify the container (such as Frame or Panel) designed to hold this component. The container can then add this component into itself via aContainer.add(aComponent) method. Every container has a add(Component) method.
14
Designing GUI GUI components are associated with events.
Separate the program GUI classes from the program processing classes. Use one class to present the graphics, another class to process the data/user input, and one class to handle the events that occur with user interaction.
15
Java APIs for GUI The Java API provides a rich selection of classes to create the visual elements of a GUI and its functional facilities. This part explores the AWT and the Swing API of classes.
16
Java APIs for GUI There are two sets of Java APIs for GUI programming:
AWT (Abstract Windowing Toolkit) and Swing. AWT APIs were introduced in JDK 1.0. (java.awt and javax.swing) Swing APIs, a much more comprehensive set of graphics libraries that enhances the AWT counterparts, has been integrated into core Java since JDK 1.2. These provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications.
17
GUI components Components form a major part of a GUI.
Java components are pre-defined standard elements such as buttons, text-fields, frames, windows, labels, scrollbars, menus, menu items, text areas, and dialog boxes. The display space on the screen is also a component.
18
GUI components Like all GUI-based applications, the Java display space is a window. Some windows have additional graphics features and are known as frame windows. Frame windows have a title, a border, and buttons for closing, minimizing, and maximizing the window.
19
GUI components Some components are used to hold other components.
For example, a dialog box can hold a label and a button. A window can hold a dialog box and other buttons and components. Components that can hold other components are called containers.
20
AWT & Swing packages These contain all of the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called a component.
21
AWT & Swing packages The Component class is the root of all AWT components. All AWT components share the properties described in the Component class.
22
AWT & Swing packages A container is a component that can contain components and other containers. A container usually has a layout manager that controls the visual placement of components in a container. The AWT package contains several layout manager classes
23
Class Hierarchy
24
Container vs. Component
25
Container vs. Component
It is important to note that in a GUI program: a component must be kept in a container. You need to identify the container and component. Every container has a method called add(Component c). E.g.:- A container (says aContainer) can invoke aContainer.add(aComponent) to add aComponent into itself.
26
Java API – Graphics class
The Graphics class has methods to draw lines, rectangles, and ovals as basic shapes. From these basic shapes, a programmer can create many complex graphics. GUI components are drawn on the screen using Graphics objects as well as the paint, repaint, and update methods. The Applet class also uses these methods to display the applet graphics in a browser window.
27
Common AWT Components A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples: buttons, checkboxes, and scrollbars of a typical graphical user interface. The Component class is the abstract superclass of the Abstract Window Toolkit components. The basic steps involved in using a GUI component are: The component object must be created with a constructor. It must be added to a container.
28
Button A Button component creates a labeled button.
An object of class Button is a push button. The application can cause some action to happen when the button is pushed. The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent The constructor of Button class: Button(String label)
29
Example: Button Button quitButton = new Button(“Quit”); setLabel() and getLabel(): Sets and gets the label associated with the button.
30
Label A Label component is a single line of text.
The text cannot be edited by the user, although it can be changed by your program. A Label class has two main constructors: Label name = new Label(“John"); //This Label constructor displays the given text. Label name = new Label(“John", Label.CENTER); //creates a label whose text is centered in the available space. getText() and setText(): Sets and gets the text for the label. name.setText(“Harry");
31
Example: Label setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
add(new Label("Hi There!")); add(new Label("Another Label")); (From Java API Docs)
32
Standard Component methods
comp.setBackground(color) and comp.setForeground(color): Sets the background and foreground colors for the component. If no colors are set for a component, it inherits the colors of its parent container. comp.setFont(font): Sets the font for the component. That font is used to display text on the component. comp.getSize(): Returns the size of the component in the form of a Dimension object. When a component is first created, its size is zero. The size will be set later, probably by a layout manager.
33
comp. getParent(): Returns the parent Container of the component
comp.getParent(): Returns the parent Container of the component. For a top-level component such as a Window or Applet, the return value will be null. comp.getLocation(): Gets the location of this component in the form of a point specifying the component's top-left corner. The location will be relative to the parent's coordinate space. comp.isEnabled(): Determines whether the component is enabled. An enabled component can respond to user input. Components are enabled initially by default. comp.setVisible(boolean b): Shows or hides the component depending on the value of parameter b. If true, shows the component; otherwise, hides the component.
34
AWT Container Classes A GUI program must begin with a top-level container. The commonly-used top-level containers in AWT are Frame, Dialog and Applet. An AWT Frame is a "window”
35
AWT Container Classes An AWT GUI program is usually written as a subclass of java.awt.Frame class. An AWT Dialog is a pop-up window used for interacting with the users. A Panel is an invisible retangular box under a higher-level container, used to group and layout a set of related UI components
36
Component creation Three steps are necessary to create and place a UI component: Declare the component with an identifier; Construct via new operator and invoke the chosen constructor; Identify the container (such as Frame or Panel) designed to hold this component. The container can then add this component into itself via aContainer.add(aComponent) method. Every container has a add(Component) method.
37
Event Handling GUIs are event driven
Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc. Class java.awt.AWTEvent
38
Event Handling Models Event Inheritance Delegation
39
Some event classes of package java.awt.event
Object EventObject AWTEvent ActionEvent AdjustmentEvent ItemEvent TextEvent ContainerEvent FocusEvent PaintEvent WindowEvent InputEvent MouseWheelEvent ComponentEvent KeyEvent MouseEvent Object ActionEvent EventObject AdjustmentEvent AWTEvent ContainerEvent ItemEvent FocusEvent TextEvent PaintEvent ComponentEvent WindowEvent InputEvent KeyEvent MouseEvent MouseWheelEvent
40
Event Handling Event-handling model Programmer must perform two tasks
Three parts Event source GUI component with which user interacts Event object Encapsulates information about event that occurred Event listener (handler) Receives event object when notified, then responds Programmer must perform two tasks Register event listener for event source Implement event-handling method (event handler)
41
Event-listener interfaces of package java.awt.event
ActionListener AdjustmentListener ComponentListener ContainerListener FocusListener ItemListener KeyListener MouseListener MouseMotionListener TextListener WindowListener «interface» EventListener «interface» ActionListener «interface» AdjustmentListener «interface» ComponentListener «interface» ContainerListener «interface» FocusListener «interface» ItemListener «interface» KeyListener «interface» MouseListener «interface» MouseMotionListener «interface» TextListener
42
Event model Components that react to user input can generate events
Events are objects that represent what happened Objects that are designed to process the event register as a listener with the event generator
43
Events & listeners Event objects represent the event
Subclass from java.awt.AWTEvent ActionEvent, TextEvent, KeyEvent, InputEvent Objects designed to process events are called handlers Handlers implement the appropriate listener interface Register with the event generator
44
Listener classes Separate listener interface for each event type
ActionListener, ItemListener, FocusListener, etc The class that will be receiving the event implements the appropriate listener The action is performed in the method specified by the interface
45
Adapter classes Some listener interfaces include many methods
To implement these interfaces the handler class must implement all these methods Java provides adapter classes, that implement empty methods The handler need only extend the adapter class and implement the method(s) of interest
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.