Source of LabelFrame.java


  1: // Fig. 11.6: LabelFrame.java
  2: // Demonstrating the JLabel class.
  3: import java.awt.FlowLayout; // specifies how components are arranged
  4: import javax.swing.JFrame; // provides basic window features
  5: import javax.swing.JLabel; // displays text and images
  6: import javax.swing.SwingConstants; // common constants used with Swing
  7: import javax.swing.Icon; // interface used to manipulate images
  8: import javax.swing.ImageIcon; // loads images
  9: 
 10: public class LabelFrame extends JFrame 
 11: {
 12:    private JLabel label1; // JLabel with just text
 13:    private JLabel label2; // JLabel constructed with text and icon
 14:    private JLabel label3; // JLabel with added text and icon
 15: 
 16:    // LabelFrame constructor adds JLabels to JFrame
 17:    public LabelFrame()
 18:    {
 19:       super( "Testing JLabel" );
 20:       setLayout( new FlowLayout() ); // set frame layout
 21: 
 22:       // JLabel constructor with a string argument
 23:       label1 = new JLabel( "Label with text" );
 24:       label1.setToolTipText( "This is label1" );
 25:       add( label1 ); // add label1 to JFrame
 26: 
 27:       // JLabel constructor with string, Icon and alignment arguments
 28:       Icon bug = new ImageIcon( getClass().getResource( "bug1.gif" ) );
 29:       label2 = new JLabel( "Label with text and icon", bug, 
 30:          SwingConstants.LEFT );
 31:       label2.setToolTipText( "This is label2" );
 32:       add( label2 ); // add label2 to JFrame
 33: 
 34:       label3 = new JLabel(); // JLabel constructor no arguments
 35:       label3.setText( "Label with icon and text at bottom" );
 36:       label3.setIcon( bug ); // add icon to JLabel
 37:       label3.setHorizontalTextPosition( SwingConstants.CENTER );
 38:       label3.setVerticalTextPosition( SwingConstants.BOTTOM );
 39:       label3.setToolTipText( "This is label3" );
 40:       add( label3 ); // add label3 to JFrame
 41:    } // end LabelFrame constructor
 42: } // end class LabelFrame
 43: 
 44: 
 45: /**************************************************************************
 46:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 47:  * Pearson Education, Inc. All Rights Reserved.                           *
 48:  *                                                                        *
 49:  * DISCLAIMER: The authors and publisher of this book have used their     *
 50:  * best efforts in preparing the book. These efforts include the          *
 51:  * development, research, and testing of the theories and programs        *
 52:  * to determine their effectiveness. The authors and publisher make       *
 53:  * no warranty of any kind, expressed or implied, with regard to these    *
 54:  * programs or to the documentation contained in these books. The authors *
 55:  * and publisher shall not be liable in any event for incidental or       *
 56:  * consequential damages in connection with, or arising out of, the       *
 57:  * furnishing, performance, or use of these programs.                     *
 58:  *************************************************************************/