Swinging Into Swing: Leo S. Primero III
Swinging Into Swing: Leo S. Primero III
Constructor Description
JFrame ( ) Creates a new frame with no
title.
JFrame (String title) Creates a new frame with
the specified title.
Method Description
void add (Component c) Adds the specified
component to the frame.
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
JMenuBar getJMenuBar ( ) Gets the menu for this
frame.
void pack ( ) Adjusts the size of the
frame to fit the components
added to it.
void remove (Component c) Removes the specified
component from the frame.
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
void remove (Component c) Removes the specified
component from the frame.
void setDefaultCloseOperation Sets the action taken when
the user closes the frame.
Always specify
JFrame.EXIT ON CLOSE.
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
void setIconImage Sets the icon displayed when the
(Icon image)
frame is minimized.
void setLayout Sets the layout manager used to
(LayoutManager layout)
control how components are
arranged when the frame is
displayed. The default is the
BorderLayout manager.
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
void setLocation Sets the x and y position of
(int x, int y)
the frame on-screen. The
top-left corner of the screen
is 0, 0.
void setLocationRelativeTo Centers the frame on-screen
(Component c)
if the parameter is null.
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
void setResizeable Sets whether or not the size of
(boolean value)
the frame can be changed by the
user. The default setting is true
(the frame can be resized).
Useful JFrame Constructors and
Methods (Cont’d)
Method Description
void setSize (int width, int Sets the size of the frame to
height)
the specified width and
height.
void Sets the menu for this
setJMenuBar(JMenuBarMenu)
frame.
Using the JPanel Class
• A panel is a type of container that's designed to hold a group
of components so they can be displayed on a frame. The
normal way to display a group of controls such as text fields,
labels, buttons, and other GUI widgets is to add those controls
to a panel, and then add the panel to the frame.
• You can bypass the panel and add the controls directly to the
frame if you want, but using a separate panel to hold the
frames control is almost always a good idea.
Useful JPanel Constructors and
Methods
Constructor Description
JPanel () Creates a new panel.
JPanel (boolean Creates a new panel. If the
isDoubleBuffered)
parameter is true, the panel
uses a technique called
double-buffering.
Useful JPanel Constructors and
Methods (Cont’d)
Constructor Description
JPanel (LayoutManager Creates a new panel with
layout)
the specified layout
manager. The default
layout manager is
FIowLayout.
Useful JPanel Constructors and
Methods (Cont’d)
Method Description
void add (Component c) Adds the specified
component to the panel.
void remove (Component c) Removes the specified
component from the
panel.
Useful JPanel Constructors and
Methods (Cont’d)
Method Description
void setLayout Sets the layout manager used to
(LayoutManager
layout) control how components are
arranged when the panel is
displayed. The default is the
FIowLayout manager.
Useful JPanel Constructors and
Methods (Cont’d)
Method Description
void setLocation (int x, Sets the x and y position of
int y)
the frame-screen. The top-
left corner of the screen is 0,
0.
Useful JPanel Constructors and
Methods (Cont’d)
Method Description
void setSize (int width, Sets the size of the frame to the
int height)
specified width and height.
void setToolTipText Sets the tooltip text that's
(String text)
displayed if the user rests the
mouse over an empty part of the
panel.
Using Labels
• A label is a component that simply displays text.
Labels are used for a variety of purposes: to
display captions for other controls such as text
fields or combo boxes, to display informational
messages, or to show the results of a calculation
or a database lookup.
Using Labels
• A label can also display an image, or it can display
both an image and some text. And you have
complete control over the appearance of the text.
• You can specify the font, size, whether the text is
bold, italic, or underlined, what color the text is
displayed as, and so on.
Useful JLabels Constructors and
Methods
Constructor Description
JLabel ( ) Creates a new label with no
initial text.
Method Description
String getText ( ) Returns the text displayed by the
label.
void setText (String Sets the text displayed by the
text)
label.
Useful JLabels Constructors and
Methods (Cont’d)
Method Description
void setToolTipText Sets the tooltip text that's
(String text)
displayed if the user rests the
mouse over the label for a few
moments.
void setVisible Shows or hides the label.
(boolean value)
Creating Buttons
• Next to labels, the Swing component used most is
the JButton component which creates a button the
user can click.
• The constructors of the JButton class are similar to
the constructors for the JLabel class. You can
either create an empty button or a button with text.
Useful JPanels Constructors and
Methods
Constructor Description
JButton ( ) Creates a new button
with no initial text.
JButton (String text) Creates a new button
with the specified text.
Useful JPanels Constructors and
Methods (Cont’d)
Method Description
doClick ( ) Triggers an action event for
the button as if the user
clicked it.
String getText () Returns the text displayed by
the button.
Useful JPanels Constructors and
Methods (Cont’d)
Method Description
void setBorderPainted Shows or hides the button's
(boolean value)
border. The default setting is true
(the border is shown).
void setContentAreaFilled Specifies whether or not the
(boolean value)
button's background should be
filled or left empty. The default
setting is true (the background is
filled in).
Useful JPanels Constructors and
Methods (Cont’d)
Method Description
void Specifies whether or not the
setContentAreaFilled
button's background should be
(boolean value)
filled or left empty. The default
setting is true (the background is
filled in).
void setEnabled (boolean Enables or disables the button. The
value)
default setting is true (enabled).
Useful JPanels Constructors and
Methods (Cont’d)
Method Description
void Enables or disables the rollover
setRolloverEnabled
(boolean value) effect, which causes the border
to get thicker when the mouse
moves over the button. The
default setting is true (rollover
effect enabled).
Useful JPanels Constructors and
Methods (Cont’d)
Method Description
void setText (String Sets the text displayed by the
text)
button.
void setToolTipText Sets the tooltip text that's displayed
(String text)
if the user lets the mouse rest over
the button.
void setVisible Shows or hides the button. The
(boolean value)
default setting is true (the button is
visible).
A Word on the Layout of
Components