【发布时间】: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。这将在整个宽度上拉伸,然后可以在面板中添加一个左对齐的标签。
-
这看起来像
<html><body style='margin: 0 0 0 0'>这实际上是我的第一个冲动。 -
当我在 HUDisplay (JPanel) 上 setMaximumSize 和 setAlignementX 时,JLabel 仍然与“中心”对齐。我不知道 body 的 style 属性是否是一个建议,但它也不起作用:-(