Java JLabel(String text, int horizontalAlignment) Constructor

Syntax

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

public JLabel(String text,  int horizontalAlignment)

Example

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


/*from  w  w  w. j  av  a  2  s . co  m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JPanel {

  public Main() {
    JLabel lblMarried = new JLabel("Are you married?", JLabel.LEFT);
    JLabel lblGolf = new JLabel("Do you play golf?", JLabel.RIGHT);
    
    add(lblMarried);
    add(lblGolf);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Main());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}