You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing中实现棋子图片JLabel在棋盘方格JPanel内垂直居中的方法

Java Swing中实现棋子图片JLabel在棋盘方格JPanel内垂直居中的方法

看起来你已经把棋盘方格的布局和代数标签的位置搞定了,就差棋子图片垂直居中的问题啦。咱们来一步步解决它~

问题原因分析

你的Square类采用了BorderLayout布局,代数标签被放在了PAGE_END(底部)区域,而棋子JLabel默认会被添加到CENTER区域。理论上BorderLayout.CENTER的组件会填充水平方向的可用空间,但如果棋子图片尺寸小于方格高度,JLabel本身不会自动让图片垂直居中——因为JLabel默认的对齐逻辑是针对自身内容,而非容器的可用空间。

解决方案

这里提供两种简单有效的方法,你可以按需选择:

方法一:直接设置棋子JLabel的对齐方式

这种方法最简洁,只需要在创建棋子JLabel时,明确设置它的水平和垂直对齐方式,让图片在JLabel内部居中。同时利用BorderLayout.CENTER区域会拉伸组件填充剩余空间的特性,确保JLabel占满方格的可用区域:

public void updatePiece(char piece) {
    removeAll();
    BufferedImage pieceImage;
    if (piece == '-') {
        setAlgebraicNotationLabel(position);
        validate();
        repaint();
        return;
    }
    String fileName = PIECE_ICON_PATH + piece + ".png";
    try {
        pieceImage = ImageIO.read(new File(fileName));
        System.out.println(position + " -> " + pieceImage.getHeight());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    /* 刷新方格内容 */
    setAlgebraicNotationLabel(position);
    // 创建棋子标签并设置居中对齐
    JLabel pieceLabel = new JLabel(new ImageIcon(pieceImage));
    pieceLabel.setHorizontalAlignment(JLabel.CENTER);
    pieceLabel.setVerticalAlignment(JLabel.CENTER);
    // 添加到CENTER区域,自动填充剩余空间
    add(pieceLabel, BorderLayout.CENTER);

    validate();
    repaint();
}

方法二:用辅助面板实现居中(兼容特殊场景)

如果因为图片尺寸或布局冲突导致第一种方法不生效,可以把棋子标签放到一个使用GridBagLayout的辅助面板中——GridBagLayout默认会将组件在面板内垂直水平都居中,再把这个面板加到BorderLayout.CENTER区域:

public void updatePiece(char piece) {
    removeAll();
    BufferedImage pieceImage;
    if (piece == '-') {
        setAlgebraicNotationLabel(position);
        validate();
        repaint();
        return;
    }
    String fileName = PIECE_ICON_PATH + piece + ".png";
    try {
        pieceImage = ImageIO.read(new File(fileName));
        System.out.println(position + " -> " + pieceImage.getHeight());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    /* 刷新方格内容 */
    setAlgebraicNotationLabel(position);
    // 创建辅助面板,使用GridBagLayout实现居中
    JPanel piecePanel = new JPanel(new GridBagLayout());
    JLabel pieceLabel = new JLabel(new ImageIcon(pieceImage));
    piecePanel.add(pieceLabel);
    // 将辅助面板添加到CENTER区域
    add(piecePanel, BorderLayout.CENTER);

    validate();
    repaint();
}

额外小提示

  • 记得在调用removeAll()后,一定要调用validate()和repaint()来触发界面的重新布局和绘制,确保修改生效。
  • 可以检查一下方格(Square)的尺寸是否足够,如果方格高度和图片高度几乎一致,居中效果会不明显哦~

备注:内容来源于stack exchange,提问作者VictoRPiles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 10:29:51