【问题标题】:JLabel not aligned to the leftJLabel 未向左对齐
【发布时间】:2018-05-08 10:12:47
【问题描述】:

我的 JLabel hudHeader 未与左对齐。

这是目前的样子:

HUDisplay 扩展了 JPanel。

public HUDisplay() {

Border border = BorderFactory.createMatteBorder(0, 1, 0, 0, new Color(128, 128, 128));
Border margin = new EmptyBorder(8,8,8,8);
setBorder(new CompoundBorder(border, margin));
setBackground(new Color(250,250,250));

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));


hudHeader = new JLabel("<html><b>Simulation informations</b><br />" +
        "<i style=\"font-size: 8px;\">Running for " + "0" + "s</i>" + "</html>");

hSeparation = new JSeparator(SwingConstants.HORIZONTAL);

add(hudHeader);
add(hSeparation);

setVisible(true);

}

【问题讨论】:

  • stackoverflow.com/questions/9212155/… 作为 BoxLayout 中心
  • 我已经尝试过了,以下几行没有任何变化:-( setMaximumSize( new Dimension( 200, 600) ); setAlignmentX(Component.LEFT_ALIGNMENT);
  • 只是添加了 JPanel。这将在整个宽度上拉伸,然后可以在面板中添加一个左对齐的标签。
  • 这看起来像 &lt;html&gt;&lt;body style='margin: 0 0 0 0'&gt; 这实际上是我的第一个冲动。
  • 当我在 HUDisplay (JPanel) 上 setMaximumSize 和 setAlignementX 时,JLabel 仍然与“中心”对齐。我不知道 body 的 style 属性是否是一个建议,但它也不起作用:-(

标签: java swing jlabel


【解决方案1】:

不是这样:它看起来像 HTML 正文边距

NOT hudHeader = new JLabel(
NOT         "<html><body style='margin: 0 0 0 0;'><b>Simulation informations</b><br />" +
NOT         "<i style=\"font-size: 8px;\">Running for " + "0" + "s</i>" + "</html>");

不过可能会有问题,比如 RTL,所以也试试

hudHeader .setAlignment(SwingConstants.LEFT);

【讨论】:

  • 它适用于第二个选项。我使用的是 Component.LEFT_ALIGNEMENT 而不是 SwingConstants.LEFT。感谢您的帮助。
猜你喜欢
  • 2011-11-25
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
相关资源
最近更新 更多