GUI Basics
GUI Basics
GUI Basics
2
Creating GUI Objects
// Create a button with text OK
JButton jbtOK = new JButton("OK");
Button
3
Swing vs. AWT
First Java GUI library was known as the Abstract Windows Toolkit (AWT).
AWT is fine for developing simple graphical user interfaces, but not for
complex GUI projects.
Swing components are less dependent on the target platform and use less of
the native GUI resource.
4
Swing - Container Classes
5
GUI API - Container Classes
6
GUI API - Helper Classes
7
Use AWT or SWING classes?
8
Swing GUI Components
JCheckBoxMenuItem
JMenuItem JMenu
JToggleButton JCheckBox
JRadioButton
JComponent JEditorPane
JTextArea
TextArea
Graphics List
Component Choice
CheckBox
LayoutManager CheckBoxGroup
Canvas
MenuBar
Scrollbar
10
Frames
To create a user interface, you need to create either a frame or an applet
to hold the user- inter-face components.
Frame is a window that is not contained inside another window.
Frame is the basis to contain other user interface components in Java
GUI applications.
The JFrame class can be used to create windows.
For Swing GUI programs, use JFrame class to create widows.
11
JFrame Class
12
Example1: Creating Jframes
import javax.swing.*;
frame.setVisible(true);
Title bar,
Minimize,
} Maximize,
} Close btn.
JFrame
import javax.swing.*;
frame.setVisible(true);
}
}
14
JFrame Class
15
Layout Managers
UI components are placed in containers.
Each container has a layout manager to arrange the UI
components within the container.
Layout managers are set in containers using the
setLayout(LayoutManager) method in a container.
16
The FlowLayout Class
17
Example3: FlowLayout
18
Example3: FlowLayout
import java.awt.FlowLayout;
import javax.swing.*;
}
}
19
Example4: GridLayout
3x2
20
The GridLayout Class
21
Example4: GridLayout
public class MyFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setSize(400, 300); 1
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23
The BorderLayout Manager
24
Example5: BorderLayout Manager
This version
places a JButton
in each region of
a BorderLayout
25
Example5: BorderLayout Manager
26
The Color Class
RGB Colors are made of red, green, and blue components, each
intensity is represented by a byte value
0 (darkest shade)
255 (lightest shade).
Red
Green
Example: Blue
27
Standard Colors
A number of standard colors are defined as constants in
java.awt.Color.
28
Setting Colors
You can use the following methods to set the component’s
background and foreground colors:
setBackground(Color c)
setForeground(Color c)
Example:
The button jBtn shows red text on a yellow background
jBtn.setBackground(Color.YELLOW);
jBtn.setForeground(Color.RED);
29
The Font Class
Font Names
Supported in all platforms: Font.PLAIN (0),
Font.BOLD (1),
SansSerif, Serif, Font.ITALIC (2),
Monospaced, Font.BOLD +
Dialog, Font.ITALIC (3)
DialogInput.
Example:
Font myFont1 = new Font("SansSerif", Font.BOLD, 16);
Agency FB
Aharoni ...
Algerian
Andalus Batang
Angsana New BatangChe
AngsanaUPC Bauhaus 93
Aparajita Bell MT
Arabic Typesetting Berlin Sans FB
Arial Berlin Sans FB Demi
Arial Black Bernard MT Condensed
Arial Narrow Blackadder ITC
Arial Rounded MT Bold Wingdings 3
Arial Unicode MS ZWAdobeF
31
Baskerville Old Face
Using Panels as Sub-Containers
Panels act as sub-containers for grouping user interface
components.
It is recommended that you place the user interface
components in panels and place the panels in a frame.
You can also place panels in a panel.
To add a component to JFrame, you actually add it to the
content pane of JFrame.
To add a component to a panel, you add it directly to the
panel using the add method.
32
Example6: Testing Panels
This example uses panels to organize components.
The program creates a user interface for a Microwave oven.
frame
A textfield
p2
A button 12
buttons p1
33
Example6: Testing Panels
frame.add(p2, BorderLayout.CENTER);
p1.add(new JButton("0"));
}
p1.add(new JButton("Start"));
p1.add(new JButton("Stop")); }
34
Common Features of Swing Components
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
java.awt.Component
-font: java.awt.Font The font of this component.
-background: java.awt.Color The background color of this component.
-foreground: java.awt.Color The foreground color of this component.
-preferredSize: Dimension The preferred size of this component.
-visible: boolean Indicates whether this component is visible.
+getWidth(): int Returns the width of this component.
+getHeight(): int Returns the height of this component.
+getX(): int getX() and getY() return the coordinate of the component’s
+getY(): int upper-left corner within its parent component.
java.awt.Container
+add(comp: Component): Component Adds a component to the container.
+add(comp: Component, index: int): Component Adds a component to the container with the specified index.
+remove(comp: Component): void Removes the component from the container.
+getLayout(): LayoutManager Returns the layout manager for this container.
+setLayout(l: LayoutManager): void Sets the layout manager for this container.
+paintComponents(g: Graphics): void Paints each of the components in this container.
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
javax.swing.JComponent
-toolTipText: String The tool tip text for this component. Tool tip text is displayed when
the mouse points on the component without clicking.
-border: javax.swing.border.Border The border for this component.
35
Borders
You can set a border on any object of the JComponent class.
To create a titled border, use
new TitledBorder(String title)
36
Borders
Example: Modify previous example adding statements
and
TitleBorder
LineBorder
37
Test Swing Common Features
font toolTipText
background border
foreground
preferredSize
minimumSize
maximumSize
38
Test Swing Common Features
39
Image Icons
Java uses the javax.swing.ImageIcon class to represent an
icon.
Images are normally stored in image files.
Example:
the following statement creates an icon from an image file
us.gif in the image directory under the current class path:
40
Image Icons
Example: Modify Microwave GUI to add icon
ImageIcon myIcon = new ImageIcon("c://temp//Food-128.png");
btnWakeUp.setIcon(myIcon);
frame.add(btnWakeUp, BorderLayout.WEST);
icon
41
Splash Screen
A splash screen is an image that is displayed while the (slower)
application is starting up.
42
Advanced Resources
WindowBuilder
http://code.google.com/javadevtools/wbpro/
http://code.google.com/javadevtools/wbpro/quick_start.html
http://www.java-javafx.com/2011/01/windowbuilder-pro-hello-world-java.html
43