![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to set the alignment of the JLabel content along the Y axis in Java?
To set the alignment of the label content along the Y axis, use the setVerticalAlignment() method. Let us first set a label component. We have set the label background color as well so that we can check the alignment of the label’s content properly −
JLabel label = new JLabel("Product Name "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.ORANGE);
Now, we will align the label content along the Y axis −
label.setVerticalAlignment(JLabel.CENTER);
The following is an example to set the alignment of the JLabel content along the Y axis −
Example
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Our Frame - Align JLabel"); frame.setLayout(new FlowLayout()); JLabel label = new JLabel("Product Name "); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.ORANGE); Font font = new Font("Serif", Font.BOLD, 14); label.setFont(font); label.setVerticalAlignment(JLabel.CENTER); JTextArea text = new JTextArea(); text.setText("Enter product...."); font = new Font("Serif", Font.BOLD, 13); text.setFont(font); frame.add(label); frame.add(text); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
Output
Advertisements