- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to set alignment for text in JLabel
JLabel Left Aligned
The following is an example to set left alignment for JLabel −
Example
import java.awt.Font; import javax.swing.*; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Label Demo"); JLabel label; label = new JLabel("Left aligned!", JLabel.LEFT); label.setFont(new Font("Verdana", Font.PLAIN, 13)); frame.add(label); frame.setSize(500,300); frame.setVisible(true); } }
Output
JLabel Center Aligned
The following is an example to set center alignment for JLabel −
Example
import java.awt.Font; import javax.swing.*; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Label Demo"); JLabel label; label = new JLabel("Center aligned!", JLabel.CENTER); label.setFont(new Font("Verdana", Font.PLAIN, 13)); frame.add(label); frame.setSize(500,300); frame.setVisible(true); } }
Output
JLabel Right Aligned
The following is an example to set right alignment for JLabel −
Example
import java.awt.Font; import javax.swing.*; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Label Demo"); JLabel label; label = new JLabel("Right aligned!", JLabel.RIGHT); label.setFont(new Font("Verdana", Font.PLAIN, 13)); frame.add(label); frame.setSize(500,300); frame.setVisible(true); } }
Output
- Related Articles
- How to set the alignment of the JLabel content along the X axis in Java?
- How to set the alignment of the JLabel content along the Y axis in Java?
- Java Program to change JLabel text after creation
- How to set Text alignment in HTML?
- How to set alignment to text in text flow layout?
- Set Text Alignment using CSS
- How to change text font for JLabel with HTML in Java?
- Set the alignment of the JLabel content along the X axis on the right in Java
- How to set vertical alignment for a component in Java?
- How to set the alignment of the JLabel content along the X axis on the left in Java
- How to set the alignment of the JLabel content along the Y axis on the top in Java
- How to set the alignment of the JLabel content along the Y axis in the bottom with Java
- How to set the text alignment of Text using FabricJS?
- Set Text Alignment Working with CSS
- Java Program to set Horizontal Alignment of content in a JTextField

Advertisements