How to align JLabel text vertically top in Java?

Java 8Object Oriented ProgrammingProgramming

Use the setVerticalAlignment() method to align JLabel text vertically to the top:

JLabel label = label.setVerticalAlignment(JLabel.TOP);

The following is an example to align JLabel text vertically to the top:

Example

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("This is demo label!");
      label.setFont(new Font("Verdana", Font.PLAIN, 14));
      label.setVerticalAlignment(JLabel.TOP);
      Border border = BorderFactory.createLineBorder(Color.ORANGE);
      label.setBorder(border);
      frame.add(label);
      frame.setSize(600,300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 06-May-2019 11:33:49
Advertisements