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

JavaFX中BorderPane仅显示中心区域其他Pane不显示如何解决

问题原因

你的代码存在三个核心错误导致底部黑色Pane无法显示:

  • 布局区域分配完全错位:你将黑色lowerPane错误放在了BorderPane的top区域,扫雷面板middlepane错误放在了bottom区域,两者都没有放在正确的位置
  • 扫雷面板尺寸挤占全部窗口空间:你给扫雷面板设置了800*800的固定首选尺寸,直接占满了整个窗口,没有给底部Pane预留空间
  • 底部空Pane未设置高度:空的Pane默认首选高度为0,即便位置放对也不可见
修复方案

修改createContent方法中的布局设置部分即可,修改后的代码如下:

private Parent createContent() {
    BorderPane root = new BorderPane();
    Pane middlepane = new Pane();
    Pane lowerPane = new Pane();
    
    // 设置底部黑色Pane的高度,数值可自行调整
    lowerPane.setPrefHeight(60);
    lowerPane.setStyle("-fx-background-color: black;");
    // 扫雷面板放在中心区域,自动适配剩余空间
    root.setCenter(middlepane);
    // 黑色Pane放在底部区域
    root.setBottom(lowerPane);
    // 扫雷面板高度减去底部Pane的高度,避免内容被截断
    middlepane.setPrefSize(W, H - lowerPane.getPrefHeight());

    // 后续原有逻辑保持不变
    for (int y = 0; y < Y_TILES; y++) {
        for (int x = 0; x < X_TILES; x++) {
            Tile tile = new Tile(x, y, Math.random() < 0.2);

            grid[x][y] = tile;
            middlepane.getChildren().add(tile);
        }
    }

    for (int y = 0; y < Y_TILES; y++) {
        for (int x = 0; x < X_TILES; x++) {
            Tile tile = grid[x][y];

            if (tile.hasBomb)
                continue;

            long bombs = getNeighbors(tile).stream().filter(t -> t.hasBomb).count();

            if (bombs > 0)
                tile.text.setText(String.valueOf(bombs));
        }
    }

    return root;
}

修改后运行即可看到底部黑色区域正常显示,扫雷游戏在中心区域完整展示。

内容的提问来源于stack exchange,提问作者Zemelon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 08:24:02