public class LabelDemo
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
42:
43:
44: /**************************************************************************
45: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
46: * Pearson Education, Inc. All Rights Reserved. *
47: * *
48: * DISCLAIMER: The authors and publisher of this book have used their *
49: * best efforts in preparing the book. These efforts include the *
50: * development, research, and testing of the theories and programs *
51: * to determine their effectiveness. The authors and publisher make *
52: * no warranty of any kind, expressed or implied, with regard to these *
53: * programs or to the documentation contained in these books. The authors *
54: * and publisher shall not be liable in any event for incidental or *
55: * consequential damages in connection with, or arising out of, the *
56: * furnishing, performance, or use of these programs. *
57: *************************************************************************/