简体   繁体   English

如何将JLabel对齐到JPanel的左侧?

[英]How to align JLabel to the left of the JPanel?

I want to align my JLabel to the left. 我想把我的JLabel对齐到左边。

    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 it using EmptyBorder but it isnt aligning properly. 我尝试使用EmptyBorder做它,但它没有正确对齐。 I am using FlowLayout 我正在使用FlowLayout

FlowLayout uses CENTER alignment by default. FlowLayout默认使用CENTER对齐。 Try using LEFT alignment for your JLabel JPanel container 尝试为JLabel JPanel容器使用LEFT对齐

myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

You might wish to set the JLabel's horizontalAlignment property. 您可能希望设置JLabel的horizo​​ntalAlignment属性。 One way is via its constructor. 一种方法是通过它的构造函数。 Try: 尝试:

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

This can also be done via the expected setter method: 这也可以通过预期的setter方法完成:

label.setHorizontalAlignment(SwingConstants.LEFT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM