Java JLabel setHorizontalAlignment 具有不同的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11396209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java JLabel setHorizontalAlignment with different results
提问by nidomiro
I want to center an JLabel inside an BorderLayout. For now I use label.setHorizontalAlignment(SwingConstants.CENTER);
and label.setVerticalAlignment(SwingConstants.BOTTOM);
.
我想在 BorderLayout 内居中 JLabel。现在我使用label.setHorizontalAlignment(SwingConstants.CENTER);
和label.setVerticalAlignment(SwingConstants.BOTTOM);
。
here the full Code:
这里是完整的代码:
public class JSector extends JRenderPanel implements WarpGateConstants {
private Sector sector;
private JLabel jLabelSectorName;
private JLabel[] jLabelWarpGate;
public JSector(Sector s) {
super(new BorderLayout());
this.setSector(s);
setBorder(new EmptyBorder(5, 5, 5, 5));
}
@Override
public void paintView(Graphics2D g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
}
private void setSector(Sector s) {
this.sector = s;
drawSectorInfo(s);
}
public Sector getSector() {
return sector;
}
private void drawSectorInfo(Sector s) {
removeAll();
if (jLabelSectorName == null || jLabelWarpGate == null) {
this.jLabelWarpGate = new JLabel[WARPGATE_MAX_VALUE + 1];
this.jLabelWarpGate[WARPGATE_NORTH] = new JLabel("N");
this.jLabelWarpGate[WARPGATE_EAST] = new JLabel("E");
this.jLabelWarpGate[WARPGATE_SOUTH] = new JLabel("S");
this.jLabelWarpGate[WARPGATE_WEST] = new JLabel("W");
for (byte i = 0; i < jLabelWarpGate.length; i++) {
setupLabel(jLabelWarpGate[i], i);
}
this.jLabelSectorName = new JLabel("SectorName");
add(this.jLabelWarpGate[WARPGATE_NORTH], BorderLayout.NORTH);
add(jLabelWarpGate[WARPGATE_EAST], BorderLayout.EAST);
add(jLabelWarpGate[WARPGATE_SOUTH], BorderLayout.SOUTH);
add(jLabelWarpGate[WARPGATE_WEST], BorderLayout.WEST);
add(jLabelSectorName, BorderLayout.CENTER);
}
for (byte i = 0; i < jLabelWarpGate.length; i++) {
WarpGate gate = s.getWarpGate(i);
if (gate != null && gate.exists()) {
jLabelWarpGate[i].setToolTipText("TargetSector: " + gate.getTargetGridPos());
jLabelWarpGate[i].setVisible(true);
} else {
jLabelWarpGate[i].setVisible(false);
}
}
jLabelSectorName.setText(s.getName());
}
private static JLabel setupLabel(JLabel label, byte warpGateID) {
Font font = label.getFont();
font = new Font(font.getName(), Font.BOLD, font.getSize() + 2);
label.setFont(font);
label.setForeground(new Color(255, 150, 0));
label.setBackground(new Color(0, 100, 255, 150));
label.setOpaque(true);
switch (warpGateID) {
case WARPGATE_NORTH:
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.TOP);
break;
case WARPGATE_EAST:
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setVerticalAlignment(SwingConstants.CENTER);
break;
case WARPGATE_SOUTH:
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.BOTTOM);
break;
case WARPGATE_WEST:
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.CENTER);
break;
}
return label;
}
}
It works good but the West and East Gates have different Vertical positions:
效果很好,但西门和东门的垂直位置不同:
I hope you can help me to solve this problem
我希望你能帮我解决这个问题
EDIT:I think I found the Problem. I set some Lables visible(false) and this causes the problem. But the problem is still there, how do I get these Jlabels on same line.
编辑:我想我发现了问题。我设置了一些标签可见(假),这会导致问题。但问题仍然存在,我如何在同一行上获取这些 Jlabel。
回答by nidomiro
I found (one) solution that works good for me:
我找到了(一个)对我有用的解决方案:
label.setForeground(new Color(255, 255, 255, 0));
this let the Component "appear" but displays nothing => Every JLabel is on the same level
这让组件“出现”但不显示任何内容 => 每个 JLabel 都在同一级别
回答by Jacob Raihle
If there are no components in the North or South 'slot' of the BorderLayout, the East and West components will expand to fill that space, causing the center of that component to depend on whether the other components are visible or not.
如果 BorderLayout 的北或南“槽”中没有组件,则 East 和 West 组件将扩展以填充该空间,从而导致该组件的中心取决于其他组件是否可见。
Three possible solutions come to mind, listed in order of complexity:
我想到了三种可能的解决方案,按复杂程度列出:
- Show all of the labels all the time, but replace the contents to make them appear invisible if there's no gate on that edge. You would have to set heights manually to keep them consistent, but that's ok for prototyping.
- Use a different LayoutManager (though no particularly good fit comes to mind)
- Draw the sector without relying on nested components to represent gates, doing the necessary calculations by hand.
- 始终显示所有标签,但如果该边缘没有门,则替换内容以使其不可见。您必须手动设置高度以保持它们一致,但这对于原型设计来说没问题。
- 使用不同的 LayoutManager(虽然没有特别合适的想法)
- 绘制扇形而不依赖嵌套组件来表示门,手动进行必要的计算。
I would probably stick with the first option for now and transition to the third one later on.
我现在可能会坚持第一个选项,稍后过渡到第三个选项。