06 User Interface 2
06 User Interface 2
Programming 2
Prepared By
Hazel Joy M. Alvarez
USER INTERFACE 2
Content Pane
Color
Layout Manager Classes
Jpanel
JScrollPane
Learning
Objectives
• At the end of the topic session, the
students should be able to:
• Use content panes
• Apply color to UI components
• Compare different layout managers
• Use the JPanel and JScrollPane classes
Content Pane
• The JFrame class is a top-level container class of Java Swing. Each GUI
component that appears on the screen should be part of a containment
hierarchy.
• A containment hierarchy is a tree of component that has a top-level
container as its root. Every top- level container has a content pane.
• A content pane is a layer that consists of all the visible components in
the user interface of a container. It can contain components or hold other
containers.
• The content pane of a JFrame can be referenced using the
getContentPane() method.
• Sample code that uses the JFrame’s content pane:
Output:
Color
• The Color class allows you to add colors to your applications to make
them more interactive and interesting. It can be used with
setBackground() and setForeground() methods of the Component
class for better user interface design.
• Named constants that represent colors which can be used:
BLACK ORANGE
BLUE PINK
CYAN RED
DARK_GRAY WHITE
GRAY YELLOW
LIGHT_GRAY
MAGENTA
• Sample code of a program that sets the background of a JFrame’s
content pane and sets both the foreground and background colors of a
JButton:
Layout Manager Classes
• List of Layout Managers:
Layout Description
Manager
Adds components to a maximum of five (5) sections arranged in
BorderLayout north, south, east, west, and center positions
Adds components into a grid of rows and columns
GridLayout Each component is the same size
BorderLayout
Output:
GridLayout
Output:
JPanel
• A JPanel is a plain, borderless surface that can hold lightweight UI
components.
• Constructors of the JPanel Class:
• JPanel() – creates a JPanel with double buffering and a FlowLayout.
• JPanel(LayoutManager layout) – creates a JPanel with the specified
layout manager and double buffering.
• JPanel(Boolean isDoubleBuffered) – creates a JPanel with a FlowLayout
and the specified double buffering strategy.
• JPanel(LayoutManager layout, Boolean isDoubleBuffered) – creates a
Jpanel with the specified layout manager and the specified double buffering
strategy.
JPanel
• Sample Code for Jpanel:
JScrollPane
• A JScrollPane provides scroll bars along the side or the bottom of a
pane, or both, with a viewable area called a viewport.
• Constructors of the JScrollPane Class:
• JScrollPane() – creates an empty JScrollPane in which both horizontal and
vertical scroll bars appear when needed.
Output: