Minggu, 08 Januari 2012

JLabel with text and with images

1 // Fig 9.19: LabelDemo.java
2 // Demonstrates the use of labels.
3 import java.awt.BorderLayout;
4 import javax.swing.ImageIcon;
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 ); // set the size of the frame
39 application.setVisible( true ); // show the frame
40 } // end main
41 } // end class LabelDemo

Tidak ada komentar:

Posting Komentar