Set JLabel text center

Complete source code below will show you, how to make text in JLabel to center.

****************************************************************************
COMPLETE SOURCE CODE FOR : JLabelTextToCenter.java
****************************************************************************


import javax.swing.JLabel;
import javax.swing.JFrame;

import javax.swing.SwingConstants;

public class JLabelTextToCenter
{
public static void main(String[]args)
{
//Create a label with text ( I am a JLabel )
JLabel label=new JLabel("I am a JLabel");

//Make JLabel text to center
label.setHorizontalAlignment(SwingConstants.CENTER);

//Create a window using JFrame with title ( JLabel text to center )
JFrame frame=new JFrame("JLabel text to center");

//Add created label into JFrame
frame.add(label);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,400);

//Make JFrame visible
frame.setVisible(true);
}
}


****************************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************************