繁体   English   中英

JavaFX - BorderPane 未在场景/舞台中居中

[英]JavaFX - BorderPane is not centered in scene/stage

我目前正在编写一个棋盘游戏,我正在使用 java 和用于 GUI 目的的 javaFX。该游戏需要 map(游戏板)位于屏幕中间,并且各种选项和附加信息位于它周围。 我的想法是有一个 BorderPane,其中中心节点是游戏板,顶部、底部等是附加选项。

我的问题是,在启动应用程序时,BorderPane 不在舞台中央,而是稍微扩展到右侧和底部,在那里它不可见。 因此看不到我的底部节点。 奇怪的是,如果我最小化 window 并再次最大化它,一切都在它应该在的地方并且完全在舞台的范围内。

最小化和再次最大化之前的应用程序

之后(从一开始它应该看起来的样子)

我的中心节点是一个普通的窗格。 我也做 stage.setMaximize(true)。 因此 window 在启动应用程序时已经最大化,再次最小化和最大化应该没有什么不同。

这个场景的代码基本上可以归结为:

@Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        Pane pane = createGameBoard();
        pane.setId("mapPane");

        Button button = new Button("Save");

        VBox box = new VBox(0, button);
        box.setAlignment(Pos.CENTER);

        Label label = new Label("Bottom");
        label.setPrefHeight(20);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(top);
        borderPane.setBottom(bottom);
        borderPane.setCenter(center);
        borderPane.setPadding(new Insets(10));

        Scene scene = new Scene(borderPane);
        
        scene.getStylesheets().add(Objects.requireNonNull(MainGUI.class.getResource("/game.css")).toString());

        stage.setScene(scene);
    }

game.css 样式表目前只设置背景颜色。 我正在使用:Java 17、JavaFX 17。

如果您需要任何进一步的信息,我很乐意提供:)

谢谢!

编辑:要重现我的问题,请运行此代码。 单击“下一个屏幕”按钮后,应该会出现该问题。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class App extends Application {

    private Stage stage;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        stage.setTitle("App");
        stage.setMinHeight(600);
        stage.setMinWidth(800);
        stage.setWidth(java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth());
        stage.setHeight(java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        stage.setMaximized(true);
        stage.setOnCloseRequest(e -> System.exit(0));

        Button button = new Button("Next Screen");
        button.setOnAction(e -> {gameScreen();});

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(button);
        
        Scene scene = new Scene(borderPane);

        stage.setScene(scene);

        stage.show();
    }

    private void gameScreen() {
        Circle circle = new Circle(0, 0, 4);
        Pane pane = new Pane(circle);
        pane.setStyle("-fx-background-color: #00c3ff");

        Button button = new Button("Top");
        VBox vBox = new VBox(0, button);
        vBox.setAlignment(Pos.CENTER);

        Label label = new Label("Bottom");

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(vBox);
        borderPane.setCenter(pane);
        borderPane.setBottom(label);
        borderPane.setPadding(new Insets(10));

        Scene scene = new Scene(borderPane);

        stage.setScene(scene);
    }

}

我的问题是通过更改我正在使用的场景的根解决的,而不是在每次切换布局(例如从主菜单到游戏屏幕)时都创建一个新场景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM