繁体   English   中英

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

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

我想把我的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));

我尝试使用EmptyBorder做它,但它没有正确对齐。 我正在使用FlowLayout

FlowLayout默认使用CENTER对齐。 尝试为JLabel JPanel容器使用LEFT对齐

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

您可能希望设置JLabel的horizo​​ntalAlignment属性。 一种方法是通过它的构造函数。 尝试:

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

这也可以通过预期的setter方法完成:

label.setHorizontalAlignment(SwingConstants.LEFT);

暂无
暂无

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

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