Chapter 1 Intro To GUI - Part 2
Chapter 1 Intro To GUI - Part 2
2
Chapter 1
Lesson Objectives
At the end of this lesson, you should be able to
Describe the Java GUI API hierarchy
Create user interfaces using frames, panels, and
simple GUI components
Describe the role of layout managers
Use the FlowLayout, GridLayout, and BorderLayout
managers to layout components in a container
Use JPanel as subcontainers
Specify colors and fonts using the Color and Font
classes
Apply common features such as borders, tool tips,
fonts, and colors on Swing components
Use borders to visually group user-interface
components
3
4
4
manager class.
Every layout manager class implements
the LayoutManager interface.
You can set the layout manager for a
container using the
setLayout(LayoutManager) method.
5
5
FlowLayout
GridLayout
BorderLayout
Note: By default, the frames layout is
BorderLayout.
6
6
Introduction to FlowLayout
The simplest layout manager.
The components are arranged in the
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.FlowLayout;
public class ShowFlowLayout extends JFrame {
public ShowFlowLayout() {
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
add(new JLabel("First Name"));
add(new JTextField(8));
add(new JLabel("MI"));
add(new JTextField(1));
add(new JLabel("Last Name"));
add(new JTextField(8));
setTitle("ShowFlowLayout");
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
ShowFlowLayout frame = new ShowFlowLayout();
}
}
10
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-alignment: int
-hgap: int
-vgap: int
+FlowLayout()
+FlowLayout(alignment: int)
11
11
P1 Q3
12
Introduction to GridLayout
Arranges components in a grid (matrix)
formation with the number of rows and
columns defined by the constructor.
The components are placed in the grid
from left to right, starting with the first
row, then the second and so on, in the
order in which they are added.
13
13
GridLayout Constructor
The constructor of the GridLayout
manager is as follows:
14
14
Note
If you resize the frame, the layout of the
components remains unchanged.
All components are given equal size in the container
of GridLayout.
15
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.awt.GridLayout;
public class ShowGridLayout extends JFrame {
public ShowGridLayout() {
setLayout(new GridLayout(3, 2));
add(new JLabel("First Name"));
add(new JTextField(8));
add(new JLabel("MI"));
add(new JTextField(1));
add(new JLabel("Last Name"));
add(new JTextField(8));
16
setTitle("ShowGridLayout");
setSize(250, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
ShowGridLayout frame = new ShowGridLayout();
}
}
17
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-rows: int
-columns: int
-hgap: int
-vgap: int
+GridLayout()
+GridLayout(rows: int, columns: int) Creates a GridLayout with a specified number of rows and columns.
+GridLayout(rows: int, columns: int, Creates a GridLayout manager with a specified number of rows and
hgap: int, vgap: int)
columns, horizontal gap, and vertical gap.
Note
In FlowLayout and GridLayout, the order in which
the components are added to the container is
important.
It determines the location of the components in the 18
18
container.
P1 Q4
19
Introduction to BorderLayout
Divides the container into five areas: East,
South, West, North, and Center.
Components are added to a BorderLayout by
using the add(Component, index) method
where index is one of the following constants:
BorderLayout.EAST
BorderLayout.SOUTH
BorderLayout.WEST
BorderLayout.NORTH or
BorderLayout.CENTER
20
20
21
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-hgap: int
-vgap: int
+BorderLayout()
+BorderLayout(hgap: int, vgap: int) Creates a BorderLayout manager with a specified number of
horizontal gap, and vertical gap.
Note:
The North and South components can stretch
horizontally,
The East and West components can stretch vertically,
The Centre component can stretch both horizontally and
vertically to fill any empty space.
22
22
GridLayout
setRows(value);
setColumns(value);
setHgap(value);
setVgap(value);
BorderLayout
setHgap(value);
setVgap(value);
E.g:
FlowLayout flowlayout = new FlowLayout();
flowlayout.setAlignment(FlowLayout.RIGHT);
flowlayout.setHgap(10);
flowlayout.setVgap(20);
23
23
24
24
Use Panels as
Subcontainers
25
25
Frame
Panel
User interface
components (UI)
Panel
Panel
Panel
UI
UI
UI
26
26
p1.add(new JButton(ButtonName));
27
27
28
import java.awt.*;
import javax.swing.*;
public class TestPanels extends JFrame {
public TestPanels() {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
for (int i = 1; i <= 9; i++) {
buttonPanel.add(new JButton("" + i));
}
buttonPanel.add(new JButton("0"));
buttonPanel.add(new JButton("Start"));
buttonPanel.add(new JButton("Stop"));
29
30
P1
-
Q5
Q6
Q7
Q8
31
Standard Colors
13 standard colors (black, blue, cyan,
dark gray, gray, green, light gray,
magenta, orange, pink, red, white,
yellow) are defined as constants in
java.awt.Color:
BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN,
LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED,
WHITE, and YELLOW.
33
Setting Colors
You can use the following methods to set
the components background and
foreground colors:
setBackground(Color c)
setForeground(Color c)
Example:
jbt.setBackground(Color.yellow);
jbt.setForeground(Color.red);
34
35
import javax.swing.*;
Import java.awt.BorderLayout;
import java.awt.Color;
public class TestColor extends JFrame {
private JButton jbtEast = new JButton("East");
private JButton jbtWest = new JButton("West");
private JButton jbtNorth = new JButton("North");
private JButton jbtSouth = new JButton("South");
private JButton jbtCenter = new JButton("Center");
public TestColor() {
jbtEast.setBackground(Color.MAGENTA);
jbtEast.setForeground(Color.WHITE);
jbtWest.setBackground(new Color(255, 255, 255));
jbtWest.setForeground(new Color(0, 0, 0));
jbtCenter.setBackground(Color.YELLOW);
jbtCenter.setForeground(new Color(100, 50, 200));
36
add(jbtEast, BorderLayout.EAST);
add(jbtWest, BorderLayout.WEST);
add(jbtNorth, BorderLayout.NORTH);
add(jbtSouth, BorderLayout.SOUTH);
add(jbtCenter, BorderLayout.CENTER);
setTitle("TestColor");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new TestColor();
}
}
37
Font Style
Font.PLAIN (0),
Font.BOLD (1),
Font.ITALIC (2),
and Font.BOLD +
Font.ITALIC (3)
Example:
39
40
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.*;
public class TestFont extends JFrame {
public TestFont() {
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
setLayout(new FlowLayout());
JButton[] buttonArray = new JButton[fontNames.length];
add(new JLabel("Total fonts: " + fontNames.length));
41
Borders
You can set a border on any object of the
JComponent class. Swing has several types of
borders. To create a titled border, use
new TitledBorder(String title)
43
JComponent Properties
font
background
foreground
preferredSize
minimumSize
maximumSize
toolTipText
border
44
45
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class TestSwingCommonFeatures extends JFrame {
private JPanel p1 = new JPanel();
private JButton jbtLeft = new JButton("Left");
private JButton jbtCenter = new JButton("Center");
private JButton jbtRight = new JButton("Right");
public TestSwingCommonFeatures() {
jbtLeft.setBackground(Color.WHITE);
jbtCenter.setForeground(Color.GREEN);
jbtRight.setToolTipText("This is the Right button");
46
p1.add(jbtLeft);
p1.add(jbtCenter);
p1.add(jbtRight);
p1.setBorder(new TitledBorder("Three Buttons"));
Font largeFont = new Font("TimesRoman", Font.BOLD, 20);
Border lineBorder = new LineBorder(Color.BLACK, 2);
JPanel p2 = new JPanel(new GridLayout(1, 2));
JLabel jlblRed = new JLabel("Red");
JLabel jlblOrange = new JLabel("Orange");
jlblRed.setForeground(Color.RED);
jlblOrange.setForeground(Color.ORANGE);
47
jlblRed.setFont(largeFont);
jlblOrange.setFont(largeFont);
jlblRed.setBorder(lineBorder);
jlblOrange.setBorder(lineBorder);
p2.add(jlblRed);
p2.add(jlblOrange);
p2.setBorder(new TitledBorder("Two Labels"));
setLayout(new GridLayout(2, 1));
add(p1);
add(p2);
setTitle("TestSwingCommonFeatures");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
48
P1 Q9
49
Image Icons
Java uses the javax.swing.ImageIcon class to
represent an icon. An icon is a fixed-size picture;
typically it is small and used to decorate
components. Images are normally stored in
image files. You can use new
ImageIcon(filename) to construct an image
icon. For example, the following statement
creates an icon from an image file us.gif in the
image directory under the current class path:
ImageIcon icon = new ImageIcon(
getClass().getResource("images/us.gif"));
50
51
import java.awt.*;
import javax.swing.*;
public class TestImageIcon extends JFrame {
private ImageIcon usIcon = new
ImageIcon(getClass().getResource("images/us.gif"));
private ImageIcon myIcon = new
ImageIcon(getClass().getResource("images/my.jpg"));
private ImageIcon frIcon = new
ImageIcon(getClass().getResource("images/fr.gif"));
private ImageIcon ukIcon = new
ImageIcon(getClass().getResource("images/uk.gif"));
public TestImageIcon() {
setLayout(new GridLayout(1, 4, 5, 5));
add(new JLabel(usIcon));
add(new JLabel(myIcon));
add(new JButton(frIcon));
add(new JButton(ukIcon));
52
setTitle("TestImageIcon");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 100);
setVisible(true);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
53