The following example illustrates JLabel example in Java swing for beginners. This example covers all the core methods of javax.swing.JLabel class.
import javax.swing.*;
import java.awt.*;
class JLabelExample extends JFrame
{
JTextField jt1,jt2;
JLabel l1,l2,l3,l4,l5,l6;
ImageIcon i1,i2,i3,i4,i5;
Font f;
public JLabelExample()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
// Set frame properties
setTitle("JLabel Demo");
setSize(400,400);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a font
f=new Font("Myriad Pro",Font.PLAIN,22);
// Create image icons
i1=new ImageIcon("icon1.png");
i2=new ImageIcon("icon2.png");
i3=new ImageIcon("pictures.png");
i4=new ImageIcon("finder.png");
i5=new ImageIcon("picturesdisabled.png");
// Create JTextFields
jt1=new JTextField(20);
jt2=new JTextField(20);
// First constructor
// Takes no parameter
// JLabel comes with
// no icon, text and enabled
l1=new JLabel();
// Second constructor
// Takes an Icon
l2=new JLabel(i1);
// Third constructor
// Takes icon and horizontal alignment
// of the label
l3=new JLabel(i2,SwingConstants.CENTER);
// Fourth constructor
// Takes String text that comes
// on the JLabel
l4=new JLabel("Label 4");
// Fifth constructor
// Takes String text with an icon and horizontal
// alignment of label
l5=new JLabel("Label 5",i3,SwingConstants.LEADING);
// Sixth constructor
// Takes String text with horizontal alignment
l6=new JLabel("<html><h2>Label <u>6</u></h2></html>",SwingConstants.TRAILING);
// Set some text
l1.setText("Label 1");
l2.setText("Label 2");
// Get text
System.out.println("Text on l1 is "+l1.getText());
System.out.println("Text on l2 is "+l2.getText());
// Set some font
l1.setFont(f);
l2.setFont(f);
// Get font
System.out.println("Font of l1 is "+l1.getFont());
System.out.println("Font of l2 is "+l2.getFont());
// Set icon
l4.setIcon(i4);
// Set icon text gap
// Default value is 4
// This is relative to
// the alignment
l4.setIconTextGap(8);
// Get icon text gap
System.out.println("Icon text gap for l4 is "+l4.getIconTextGap());
// Set mnemonic
// When user presses this key
// with alt pressed, requestFocus()
// is called on the corresponding
// label
l1.setDisplayedMnemonic('1');
l2.setDisplayedMnemonic('2');
l6.setDisplayedMnemonic((int)'6');
// Get displayed mnemonics
System.out.println("Displayed mnemonic for l1 is "+l1.getDisplayedMnemonic());
System.out.println("Displayed mnemonic for l2 is "+l2.getDisplayedMnemonic());
System.out.println("Displayed mnemonic for l5 is "+l6.getDisplayedMnemonic());
// Set displayed mnemonic index
// The character at this index
// in JLabel will be underrlined
//l6.setDisplayedMnemonicIndex(16);
// Set vertical text position
l4.setVerticalTextPosition(SwingConstants.BOTTOM);
// Get vertical text position
System.out.println("Vertical text position for l4 "+l4.getVerticalTextPosition());
// Set horizontal text position
l4.setHorizontalTextPosition(SwingConstants.CENTER);
// Get horizontal text position
System.out.println("Horizontal text position for l4 "+l4.getHorizontalTextPosition());
// Now the text on l4 will appear
// below the icon (vertical) and at
// the center looks pretty much like desktop icon
// Set label for some component.
// Whenever the user fires the mnemonic
// requestFocus() is called on the component
// associated with the label provided that
// the component is focusable
// Otherwise, focus gets transferred to the
// label itself
l1.setLabelFor(jt1);
l6.setLabelFor(jt2);
// Set some background,foreground
// to make alignment visible
// Before setting background,
// JLabel must be made opaque
// By default isOpaque() returns false
// Set opaque
l6.setOpaque(true);
// Print is opaque
System.out.println("l6 is "+(l6.isOpaque()?"opaque":"not opaque"));
// Set background,foreground
l6.setBackground(Color.DARK_GRAY);
l6.setForeground(Color.WHITE);
// Get background,foreground
System.out.println("Background for l6 is "+l6.getBackground());
System.out.println("Foreground for l6 is "+l6.getForeground());
// Also increase size
l6.setPreferredSize(new Dimension(100,50));
// Get preferred size
System.out.println("The preferred size of l6 is "+l6.getPreferredSize());
// Set vertical alignment
l6.setVerticalAlignment(SwingConstants.TOP);
// Get vertical alignment
System.out.println("Vertical alignment of l6 is "+l6.getVerticalAlignment());
// Set horizontal alignment
l6.setHorizontalAlignment(SwingConstants.RIGHT);
// Get horizontal alignment
System.out.println("Horizontal alignment of l6 is "+l6.getHorizontalAlignment());
// Disable l5
l5.setEnabled(false);
// is enabled
System.out.println("l5 is "+(l5.isEnabled()?"enabled":"disabled"));
// The icon is also greyed out
// Alternatively, one can also
// set disabled icon
l5.setDisabledIcon(i5);
// Add all components
add(l1);
add(jt1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(jt2);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new JLabelExample();
}
});
}
}