Java Swing Tutorial - Java JLabel(String text, Icon icon, int horizontalAlignment) Constructor








Syntax

JLabel(String text, Icon icon, int horizontalAlignment) constructor from JLabel has the following syntax.

public JLabel(String text,  Icon icon,  int horizontalAlignment)

Example

In the following code shows how to use JLabel.JLabel(String text, Icon icon, int horizontalAlignment) constructor.

/*from   w  ww . j a va2s  .c o  m*/
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Main {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setTitle("JLabel Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon imageIcon = new ImageIcon("yourFile.gif");

    JLabel label = new JLabel("Mixed", imageIcon, SwingConstants.RIGHT);

    frame.add(label);
    frame.pack();
    frame.setVisible(true);
  }
}