gpt4 book ai didi

具有不同结果的Java JLabel setHorizo​​ntalAlignment

转载 作者:行者123 更新时间:2023-12-03 23:18:08 25 4
gpt4 key购买 nike

我想将 JLabel 置于 BorderLayout 的中心。现在我使用 label.setHorizo​​ntalAlignment(SwingConstants.CENTER);label.setVerticalAlignment(SwingConstants.BOTTOM);

这里是完整的代码:

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;
}

}

效果很好,但西门和东门的垂直位置不同:

West and East Gates have different Vertical positions

希望你能帮我解决这个问题

编辑: 我想我找到了问题所在。我设置了一些标签可见(假),这导致了问题。但问题仍然存在,如何让这些 Jlabels 位于同一行。

最佳答案

我找到了(一个)适合我的解决方案:

label.setForeground(new Color(255, 255, 255, 0));

这让组件“出现”但什么都不显示 => 每个 JLabel 都在同一层

关于具有不同结果的Java JLabel setHorizo​​ntalAlignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com