Java Reference
In-Depth Information
5
import javax.swing.JLabel;
6
import javax.swing.JFrame;
7
8
public class LabelDemo
9
{
10
public static void main(String[] args)
11
{
12
// Create a label with plain text
13
JLabel northLabel = new JLabel( "North" );
14
15
// create an icon from an image so we can put it on a JLabel
16
ImageIcon labelIcon = new ImageIcon( "GUItip.gif" );
17
18
// create a label with an Icon instead of text
19
JLabel centerLabel = new JLabel(labelIcon);
20
21
// create another label with an Icon
22
JLabel southLabel = new JLabel(labelIcon);
23
24
// set the label to display text (as well as an icon)
25
southLabel.setText( "South" );
26
27
// create a frame to hold the labels
28
JFrame application = new JFrame();
29
30
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
31
32
// add the labels to the frame; the second argument specifies
33
// where on the frame to add the label
34
application.add(northLabel, BorderLayout.NORTH );
35
application.add(centerLabel, BorderLayout.CENTER );
36
application.add(southLabel, BorderLayout.SOUTH );
37
38
application.setSize( 300 , 300 );
39
application.setVisible( true );
40
} // end main
41
} // end class LabelDemo
Fig. 9.13 | JLabel with text and with images. (Part 2 of 2.)
Lines 3-6 import the classes we need to display JLabel s. BorderLayout from package
java.awt contains constants that specify where we can place GUI components in the
Search WWH ::




Custom Search