Introduction to Java Swing

LabelFrame.java


import java.awt.FlowLayout; // loads images
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
 public class LabelFrame extends JFrame
 {
 private JLabel label1; // JLabel with just text
 private JLabel label2; // JLabel constructed with text and icon
 private JLabel label3; // JLabel with added text and icon

 // LabelFrame constructor adds JLabels to JFrame
 public LabelFrame()
 {

super( "Testing JLabel" );
 setLayout( new FlowLayout() );
 label1 = new JLabel( "Label with text" );
label1.setToolTipText( "This is label1" );
add( label1 );
Icon bug = new ImageIcon( getClass().getResource( "bug.jpg" ) );
label2 = new JLabel( "Label with text and icon", bug,
SwingConstants.LEFT );
label2.setToolTipText( "This is label2" );
add( label2 );
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" );
 add( label3 ); // add label3 to JFrame
 } // end LabelFrame constructor
 }






LabelFrameTest.java
import javax.swing.JFrame;

public class LabelFrameTest
 {
 public static void main( String[] args )
 {
 LabelFrame labelFrame = new LabelFrame(); // create LabelFrame
 labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 labelFrame.setSize( 1513, 1815 ); // set frame size
 labelFrame.setVisible( true ); // display frame
 } // end main
 }


Comments