Java Reference
In-Depth Information
19
super
(
"Testing JLabel"
);
20
setLayout(
new
FlowLayout());
// set frame layout
21
22
// JLabel constructor with a string argument
label1 =
new
JLabel(
"Label with text"
);
label1.setToolTipText(
"This is label1"
);
23
24
25
add(label1);
// add label1 to JFrame
26
27
// JLabel constructor with string, Icon and alignment arguments
Icon bug =
new
ImageIcon(getClass().getResource(
"bug1.png"
));
label2 =
new
JLabel(
"Label with text and icon"
, bug,
SwingConstants.LEFT
);
label2.setToolTipText(
"This is label2"
);
28
29
30
31
32
add(label2);
// add label2 to JFrame
33
34
label3 =
new
JLabel();
// JLabel constructor no arguments
label3.setText(
"Label with icon and text at bottom"
);
label3.setIcon(bug);
// add icon to JLabel
label3.setHorizontalTextPosition(
SwingConstants.CENTER
);
label3.setVerticalTextPosition(
SwingConstants.BOTTOM
);
label3.setToolTipText(
"This is label3"
);
35
36
37
38
39
40
add(label3);
// add label3 to JFrame
41
}
42
}
// end class LabelFrame
Fig. 12.6
|
JLabels
with text and icons. (Part 2 of 2.)
1
// Fig. 12.7: LabelTest.java
2
// Testing LabelFrame.
3
import
javax.swing.JFrame;
4
5
public
class
LabelTest
6
{
7
public
static
void
main(String[] args)
8
{
9
LabelFrame labelFrame =
new
LabelFrame();
10
labelFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
labelFrame.setSize(
260
,
180
);
12
labelFrame.setVisible(
true
);
13
}
14
}
// end class LabelTest
Fig. 12.7
|
Testing
LabelFrame
.