How to align JLabel to the left of JPanel?

I want to align JLabel to the left.

String lText = "<html><b><font color = white face = comic sans ms size = 20>mybook</font></b></html>"; JLabel label = new JLabel(lText); label.setBorder(new EmptyBorder(25,0,25,500)); 

I tried to do this using EmptyBorder, but it does not align correctly. I am using FlowLayout

+6
source share
2 answers

FlowLayout defaults to CENTER alignment. Try using LEFT alignment for JLabel JPanel container

 myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
+13
source

You might want to set the JLabel horizontalAlignment property. One way is through its constructor. Try:

 JLabel label = new JLabel(lText, SwingConstants.LEFT); 

This can also be done using the expected installation method:

 label.setHorizontalAlignment(SwingConstants.LEFT); 
+5
source

Source: https://habr.com/ru/post/948885/


All Articles