- Trending Categories
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
Set the content of the label horizontally and vertically centered in Java
To simply set the content of the label horizontally and vertically centered, use the constant CENTER.
JLabel label = new JLabel("Best IDE", JLabel.CENTER);
The following is an example to set the content of the lable horizontally and verticaly centered −
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("Frame"); frame.setLayout(new FlowLayout()); JLabel label = new JLabel("Best IDE", JLabel.CENTER); label.setPreferredSize(new Dimension(190, 100)); label.setOpaque(true); label.setBackground(Color.darkGray); label.setForeground(Color.WHITE); Font font = new Font("Serif", Font.BOLD, 18); label.setFont(font); JTextArea text = new JTextArea(); text.setText("EclipseIDE"); font = new Font("Serif", Font.BOLD, 14); text.setFont(font); frame.add(label); frame.add(text); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to set the text of the JLabel to be right-justified and vertically centered in Java?
- How to set the content of the label to be right-justified and top-aligned in Java?
- Repeat the background image both horizontally and vertically with CSS
- Set LabelValue value and label in Java
- Set the content of the JLabel to be left-justified and top-aligned in Java
- How do I center text horizontally and vertically in a TextView of Android?
- How to center an element vertically and horizontally with CSS?
- 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?
- Set the alignment of the JLabel content along the X axis on the right in Java
- How to print the Y-axis label horizontally in a Matplotlib/Pylab chart?
- Set the flex items horizontally with CSS
- Java Program to set the content of the JLabel to be right-justified and bottom-aligned
- Java program to set the content of the JLabel to be left-justified and bottom-aligned
- How to center a button element vertically and horizontally with CSS?

Advertisements