Java Reference
In-Depth Information
The
Insets
Class
Objects of the class
Insets
are used to specify the size of the margin in a button or menu
item. The
Insets
class is in the package
java.awt
. The parameters in the following con-
structors are in pixels.
Constructor
public
Insets(
int
top,
int
left,
int
bottom,
int
right)
EXAMPLES
aButton.setMargin(
new
Insets(10, 20, 10, 20));
setIcon
and
setText
The method
setIcon
can be used to add an icon to a
JButton
,
JMenuItem
, or
JLabel
. The
argument to
setIcon
must be an
ImageIcon
object.
SYNTAX
Component
.setIcon(
ImageIcon_Object
);
The
Component
can be a
JButton
,
JMenuItem
, or
JLabel
.
EXAMPLE
JLabel helloLabel =
new
JLabel("Hello");
ImageIcon dukeIcon =
new
ImageIcon("duke_waving.gif");
helloLabel.setIcon(dukeIcon);
The method
setText
can be used to add text to a
JButton
,
JMenuItem
, or
JLabel
.
SYNTAX
Component
.setText(
Text_String
);
The
Component
can be a
JButton
,
JMenuItem
, or
JLabel
.
EXAMPLE
ImageIcon dukeIcon =
new
ImageIcon("duke_waving.gif");
JLabel helloLabel =
new
JLabel(dukeIcon);
helloLabel.setText("Hello");
The two examples are equivalent.