Swing & GUI Design
Swing & GUI Design
AWT
Swing
GUI Design Using IDE Tool
What is GUI
Graphic User Interface
Outer Interface of Program
GUI is Built by GUI Components
GUI Component : Visual Object to Interact With Out
side such as Mouse, Keyboard, etc.
Ex) AWT, Swing Components
Interfaces Provided by Java
Graphic : line, rectangle, polygon, arc, ….
Interaction Elements : button, textfield, menu, …
Image : image files
GUI and AWT
Abstract Window Toolkit (AWT)
Java.awt package
Component Class : Button, List, Menu, TextArea,
Window, MenuBar, Dialog
Graphics Related Classes : Graphics Context, Fon
t , Color
Event Class : For Event Handling
Layout Management Class : Size and Position of C
omponent
GUI Component Hierarchy
GUI Operation Outline
Procedure for Making GUI in Java
Select the Container Component and Arrange the
Basic Components within this container. Select the
Layout Manager Also.
Make the Event Handling Routine and Register it.
Event Listener
Insert the Drawing Code to Override the paint()
method in order to draw some figure on component
Paint(), repaint(), update()
Make the GUI Component
Procedure to put the GUI components into Frame or P
anel which is container
Make the container object
Declare and create the component object.
Put the objects created into container
Layout Manager
FlowLayout : From Left to Right in Panel, Default Manager
GridLayout : With Grid, Arrange in Lattice Type
GridBagLayout : Arrange Different Size Component on Grid
BorderLayout : Up, Down, Left, Right, Center
CardLayout : Arrange Several Cards
Graphics User Interface (I)
Component Class
Abstract Class which provides :
Methods to control size and position on the screen
Methods to setup cursor and return the cursor
position
Methods for drawing such as
paint(),update(),repaint()
Methods to determine keyboard input, visibility
Function for providing event handling registration
Methods related with Font and Color
Graphics User Interface (I)
Label
Not able to change
Describes Text information
Button
When user presses
Occur events to make some action.
Check Box
Has two states of ON and OFF
When user selects one of two
For multiple selection, CheckboxGroup can be used
Graphics User Interface (I)
Choice Button
Select one of several items to select
When user clicks arrow button, then the selection list is
displayed.
When user select one, Action event occurred.
Select List
Used to select one or more item(s)
Text Field
A line of text area to input
It is possible to disable/enable input
Text Area
Multi-lines of text area to input
Graphics User Interface (I)
TextComponent Class
Super class of TextField and TextArea
Scroll Bar
To input an integer within some range
Canvas
To create display area for graphic
Building a customized component by user
Graphics User Interface (II)
Panel
Inherited from Container class (Super of Applet)
Components can be added into Panel
Frame
Window to have Title Bar and Border
Independent of Window( compare to Dialog)
Dialog Box
Window to have Title Bar
Dependent on Parent Window
Modal Dialog (Can do work only after terminating the Dialog)
Non-Modal Dialog
Graphics User Interface (II)
Scroll Pane
To make an area to be scrolled easily
To display the child component as large as possible
If child component is larger than Scroll Pane, it adds the hor
izontal or vertical scroll bar.
Menu
MenuItem : Each Item of Menu
CheckboxMenuItem
Menu
MenuBar
PopupMenu
Events
Event
Event Listener
Delegation Model
Adaptor
Low Level Event :
Mouse, MouseMotion, Key, Component, Container,
Focus, Window
High Level (Have Semantics) Event:
Action, Adjustment, Item, Text
Events
JFC & Swing
Handling Events
Make Event Handler
Ex)
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { numClicks++; label.se
tText(labelPrefix + numClicks); }
}); ...
frame.addWindowListener(new WindowAdapter() { public void wind
owClosing(WindowEvent e) { System.exit(0); }
});
Swing Application Code
Demonstration