Chapter 1 Graphical User Interface (GUI)
Chapter 1 Graphical User Interface (GUI)
Flexible.
• Platform-independent provide a common set of
tools for graphical user interface design that work
on a variety of platforms.
Straightforward
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 GUI involves creating components,
putting them into containers, and arranging for
the program to respond to events.
Creating a Graphical User Interface
Components are objects, so they’re created by
invoking a constructor.
A button would be created by using a constructor
belonging to the Button class.
The most commonly used constructor has one
argument (the button’s label):
Button b = new Button("Testing");
For a component to be visible, it must be added to
a container (typically a frame) by the add method.
Creating a Graphical User Interface
To detect when an event occurs, a special “listener”
object can be attached to a component.
When the user performs an action that involves the
component, a method belonging to the listener
object will be called automatically.
List of components (example):
Button
Scrollbar
Textfield
Textarea
Checkbox
AWT Hierarchy
Use AWT in Java Programs
AWT Packages
AWT is huge! It consists of 12 packages (Swing is even bigger, with
18 packages as of JDK 1.7!). Fortunately, only 2 packages -
java.awt and java.awt.event - are commonly-used.
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, Panel, Dialog and ScrollPane)
Layout managers (such
as FlowLayout, BorderLayout and GridLayout),
Custom graphics classes (such as Graphics, Color and Font).
Use AWT in Java Programs
AWT Packages – cont..
The java.awt.event package supports event handling:
Event classes (such
as ActionEvent, MouseEvent, KeyEvent and WindowEvent),
Event Listener Interfaces (such
as ActionListener, MouseListener, KeyListener and
WindowListener)
Event Listener Adapter classes (such
as MouseAdapter, KeyAdapter, and WindowAdapter).
AWT provides a platform-independent and device-
independent interface to develop graphic programs that runs
on all platforms, such as Windows, Mac, and Linux.
AWT Container Classes
Top-Level Containers:
The
Frame object still exists; it can be made to
reappear later by calling setVisible again.
Create and Set the Frame Windows
The FrameTest program creates a Frame object
and displays it on the screen.
This program illustrates three key steps:
1. Using the Frame constructor to create a frame.
2. Setting the size of the frame.
3. Displaying the frame on the screen.
Creating a Plain Frame
// Displays a frame on the screen.
// WARNING: Frame cannot be closed.
import java.awt.*;
public class FrameTest {
public static void main(String[] args) {
Frame f = new Frame("Frame Test");
f.setSize(150, 100);
//for full screen
f.setExtendedState(FRAME.MAXIMIZE_BOTH);
f.setVisible(true);
} }
Creating a Plain Frame
Frame created by the FrameTest program.
As with the other AWT components, the appearance of
a frame depends on the platform.
Clicking on the Close button has no effect, because
there’s no action associated with that button.
The frame will have be closed the hard way, by killing
the program.
In Windows, click on the DOS window from which the
program was launched, hold down the Ctrl key, and
press the letter C.
Setting the Location of a Frame
By default, all windows (including frames) are displayed
in the upper-left corner of the screen, which has
coordinates (0, 0).
The setLocation method can be used to specify a
different location:
f.setLocation(50, 75);
Menu
MenuItem
separator
Sequence for Creating Menus
1. Create a MenuBar
object and attach it
to a frame.
3. Create MenuItem
objects and add them
to the Menu object.
Panels
Summary of panels
needed:
import java.awt.*;
// Frame class
class ButtonTestFrame extends Frame {
public ButtonTestFrame(String title) {
super(title);
setLayout(new FlowLayout());
Button b = new Button("Testing");
add(b);
}
}
44
GUI Appearance
GUI Appearance
In particular, we concentrate on the following
elements/components to create an attractive GUI:
Text. How can we ensure that the text is legible? Which font should
we use? How long should the lines be?
Images. What are the different types of image? How do you choose
the right one?
Sound. When can sound be useful? What are the different categories
of sound and when should each be used?
Mixes Media Elements to Enhance the
47
GUI Appearance
GUI Appearance
GUI Appearance
GUI Appearance
GUI Appearance
GUI Appearance