To center a JLabel
horizontally in a container in Java, you can use the setHorizontalAlignment()
method of the JLabel
class and pass it the SwingConstants.CENTER
value.
Here's an example of how to do it:
JLabel label = new JLabel("My Label"); label.setHorizontalAlignment(SwingConstants.CENTER);Suorce:www.lautturi.com
This will cause the JLabel
to be centered horizontally within its parent container.
You can also use the setAlignmentX()
method of the JLabel
class and pass it the Component.CENTER_ALIGNMENT
value to center the label horizontally within its parent container.
For example:
JLabel label = new JLabel("My Label"); label.setAlignmentX(Component.CENTER_ALIGNMENT);