【问题标题】:Java JLabel setHorizontalAlignment with different resultsJava JLabel setHorizo​​ntalAlignment 结果不同
【发布时间】:2012-07-08 22:04:34
【问题描述】:

我想在 BorderLayout 中将 JLabel 居中。现在我使用label.setHorizontalAlignment(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;
}

}

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

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

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

【问题讨论】:

  • 我错过了JRenderPanelSectorWarpGateConstantsWarpGate。如果您让不可见的标签可见但为空,这有帮助吗?
  • JRenderPanel 覆盖了 paintComponent(),Sector 和 WarpGate 只包含要显示的数据,而 WarpGateConstants 包含识别每个 WarpGate 的常量

标签: java swing alignment jlabel border-layout


【解决方案1】:

如果 BorderLayout 的北或南“槽”中没有组件,则 East 和 West 组件将扩展以填充该空间,导致该组件的中心取决于其他组件是否可见。

想到了三种可能的解决方案,按复杂程度排列:

  • 始终显示所有标签,但如果该边缘没有门,则替换内容以使它们看起来不可见。您必须手动设置高度以保持一致,但这对于原型设计来说是可以的。
  • 使用不同的 LayoutManager(虽然没有特别合适的想法)
  • 在不依赖嵌套组件表示门的情况下绘制扇形,手动进行必要的计算。

我现在可能会坚持第一个选项,稍后过渡到第三个选项。

【讨论】:

  • 我试过 setText("") 但结果和 setVisible(false) 一样
  • @niccomatik setText("") 会给你一个空标签,它不需要任何空间。 setForeground 就像你注意到的那样不会改变尺寸偏好,但仍然会给你一个“隐形”标签。
【解决方案2】:

我找到了(一个)对我有用的解决方案:

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

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

【讨论】:

    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多