0% found this document useful (0 votes)
16 views

Graphical User Interface GUI

Uploaded by

jadeoabalos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Graphical User Interface GUI

Uploaded by

jadeoabalos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

OBJECT ORIENTED

PROGRAMMING
(OOP)
GUI
What is GUI?
• GUI, which stands for Graphical User Interface, is a user-
friendly visual experience builder for Java Applications. It
comprises graphical units like buttons, labels, windows, etc. via
which users can connect with an application.
Java Abstract Window Toolkit
(AWT) Graphical Components
• The Abstract Window Toolkit (AWT) is Java’s original platform-independent
windowing graphics, and user-interface widget toolkit, preceding Swing. The
AWT is part of the Java Foundation Classes-the standard API for providing a
graphical user interface for a Java program. With the AWT, you can use the
Java GUI components like button, textbox, etc. from the library and do not
have to create the components from scratch.
• java.awt - Contains the main superclasses for the GUI components plus a
number of utility type classes, such as Color and Point.
• java.awt.event - Contains the classes and interfaces for managing
events from the GUI components, e.g. button clicks and mouse movements.
• java.swing - Contains most of the visible GUI components that are used
such as buttons, text fields, frames and panels.
AWT Packages

1. The java.awt package contains the core AWT graphics classes:


• GUI Component classes, such as Button, TextField, and Label.
• GUI Container classes, such as Frame and Panel.
• Layout managers, such as FlowLayout, BorderLayout and GridLayout.
• Custom graphics classes, such as Graphics, Color and Font
2. The java.awt.event – package supports event handling:
• Event classes, such as ActionEvent, MouseEvent, KeyEvent, and
WindowEvent,
• Event Listener Interfaces, such as ActionListener, MouseListener,
MouseMotionListener, KeyListener, and WindowListener,
• Event Listener Adapter classes, such as MouseAdapter, KeyAdapter, and
WindowAdapter.
AWT Containers and Components
There are two groups of GUI elements:
1. Component (Widget, Control): Components re elementary
GUI entities, such as Button, Label, and TextField.
They are also called widgets, controls in other graphics
systems.
2. Container: Containers, such as Frame and Panel, are used
to hold components in a specific layout (such as
FlowLayout or GridLayout). A container can also hold
sub-containers.
• JTextField: A single line of user-editable text.
• Jbutton: A button that can be clicked to cause an action.
• Jlabel: A piece of non-editable text on the screen.
• JCheckBox: Small round or square boxes that can be clicked on to indicate the selection of something, such
as an option of some sort.
• JRadioBox: Clicking the radio button will execute a particular type of event.
• JComboBox: A drop-list of items.
• Jlist: Displays a set of objects and allows the user to select one or more items.
• Jmenu: a popup window containing JMenuItem that is displayed when the user selects an item on the
JMenuBar.
• JMenuBar: is an implementation of a menu bar.
• JMenuItem: is an implementation of an item in a menu.
• JColorChooser: is used to create a color chooser dialog box.
• JFileChooser: provides a simple mechanism for the user to choose a file.
• Jtable: used to display or edit two-dimensional data that is having both rows and columns.
• Jtree: A control that displays a set of hierarchical data as an outline.
In the above figure, there are three containers: a Frame and two Panels.
▪ A Frame is the top-level container of an AWT program. A Frame has a title bar
(containing an icon, a title, and the minimize/maximize/close buttons), an optional
menu bar and the content display area.
▪ A Panel is a rectangular area used to group related GUI components in a certain
layout. In the above figure, the top-level Frame contains two Panels. There are five
components: a Label (providing description), a TextField (for users to enter text),
and three Buttons (for user to trigger certain programmed actions).
AWT Container Classes
Top-Level Containers: Frame, Dialog and Panel

1. A Frame provides the “main window”


for your GUI application. It has a title
bar (containing an icon, a title, the
minimize, maximize/restore-down and
close buttons), an optional menu bar,
and the content display area. To write
a GUI program, we typically start with
subclass extending from
java.awt.Frame to inherit the main
window as follows:
AWT Container Classes
Top-Level Containers: Frame, Dialog and Applet

2. An AWT Dialog is a “pop-up window”


used for interacting with the users. A
Dialog has a title-bar (containing an
icon, a title and a close button) and a
content display area, as illustrated.
AWT Container Classes

Secondary Containers: Panel and ScrollPane


Secondary containers are placed inside a top-level container or another
secondary container. AWT provides these secondary containers.
• Panel: a rectangular box used to
layout a set of related GUI components
in pattern such as grid or flow.

• ScrollPane: provides automatic horizontal and/or vertical scrolling for a


single child component.
AWT Component Classes
AWT provides many ready-made and reusable GUI
components in package java.awt. The frequently-used are:
Button, TextField, Label, Checkbox, CheckboxGroup
(radio buttons), List, and Choice, as illustrated below.
AWT GUI Component java.awt.Label
A java.awt.Label provides a descriptive text string. Take
note that System.out.println() prints to the system
console, NOT to the graphics screen. You could use Label to
label another component (such as text field) to provide a text
description.
CODE AND RUN
The following are the steps to create GUI in
Java
• Copied this code in to an editor.
• Save and compile the code.
The following are the steps to create GUI in
Java
• Adding buttons to the above frame. To create a component in Java, the user is
required to create an object of that component’s class. We have already
understood the container class JFrame.
• One such component to implement is JButton. This class represents the clickable
buttons. In any application or program, buttons trigger user actions. Literally,
every action begins with a click; like to close an application button.
• A swing can also be inserted, which can feature a text, a graphical icon or a
combination of both. A user can use the following constructors.
• JButton(String): This button is labelled with a specified text.
• Jbutton(Icon): This button is labelled with graphical icon.
• Jbutton(String,Icon): This button is labelled with a combination of text and icon.
The following are the steps to create GUI in
Java
• Copied this code in to an editor.
• Save and compile the code.
Code for creating a chat frame:
Output for creating a chat frame:
END

You might also like