当 .fxml 不在场景中时,JavaFX 按钮消失

JavaFX buttons disappear when .fxml not root in scene

所以我有这个 .fxml:

<StackPane fx:controller="controller.MainController" xmlns:fx="http://javafx.com/fxml">

    <Pane fx:id="aDrawPane" prefHeight="8000" prefWidth="10000" minWidth="10000" minHeight="8000">

    </Pane>
    <BorderPane fx:id="aBorderPane">
        <top>
            <VBox>
                <ToolBar fx:id="aToolBar" orientation="HORIZONTAL">
                    <HBox fx:id="umlBox">
                        <Button text="Create" fx:id="createBtn"/>
                        <Button text="Package" fx:id="packageBtn"/>
                        <Button text="Edge" fx:id="edgeBtn"/>
                        <Button text="Draw" fx:id="drawBtn"/>
                    </HBox>
                    <HBox fx:id="utilBox">
                        <Button text="Select" fx:id="selectBtn"/>
                        <Button text="Move" fx:id="moveBtn"/>
                    </HBox>
                    <HBox fx:id="undoBox">
                        <Button text="Delete" fx:id="deleteBtn"/>
                        <Button text="Undo" fx:id="undoBtn"/>
                        <Button text="Redo" fx:id="redoBtn"/>
                    </HBox>
                    <HBox fx:id="recognizeBox">
                        <Button text="Recognize" fx:id="recognizeBtn"/>
                    </HBox>
                </ToolBar>
            </VBox>
        </top>
        <bottom>
            <ToolBar>
                <Pane HBox.hgrow="ALWAYS" />
                <VBox alignment="CENTER">
                    <Slider fx:id="zoomSlider" min="10"  max="200" value="100"/>
                    <Label text="Zoom"/>
                </VBox>
                <Pane HBox.hgrow="ALWAYS" />
            </ToolBar>
        </bottom>
    </BorderPane>

    <stylesheets>
        <URL value="@main.css" />
    </stylesheets>
</StackPane>

还有我的应用程序启动器class:

public class Launcher extends Application {

    public void start(Stage stage) throws IOException { 
        BorderPane tabView = null;
        FXMLLoader loader;

        StackPane canvasView = null;
        try {
            loader = new FXMLLoader(getClass().getClassLoader().getResource("view.fxml"));
            canvasView = (StackPane) loader.load();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        Scene scene = new Scene(canvasView, 1000, 800);

        stage.setScene(scene);
        stage.show();
    }
}

有了这个,顶部工具栏中的按钮和底部工具栏中的滑块一样可见且有效。

但是,如果我将代码更改为以下内容:

Group root = new Group();
root.getChildren().add(canvasView);
Scene scene = new Scene(root, 1000, 800);

顶部工具栏可见,但按钮不可见且不可点击,底部工具栏完全消失(第一个窗格 "aDrawPane" 正常工作)。 当我想将 view.fxml 放在选项卡中时,我遇到了这个问题。 为什么会发生这种情况?如何使工具栏和按钮再次可见?

原因是组不会调整其子项的大小,而是以首选大小呈现它们。

FXML 指出窗格应该是

prefHeight="8000" prefWidth="10000" minWidth="10000"

这正是您所得到的。