简体   繁体   English

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

[英]JavaFX buttons disappear when .fxml not root in scene

So I have this .fxml: 所以我有这个.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>

And my application launcher 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();
    }
}

With this the buttons in the top toolbar are visible and working as well as the slider in the bottom one. 使用此功能,顶部工具栏中的按钮可见,并且与底部的滑块一样有效。

BUT, if I change the code to the following: 但是,如果我将代码更改为以下内容:

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

The top toolbar is visible but the buttons are invisible and non-clickable and the bottom toolbar is completely disappeard (and the first pane "aDrawPane" is working as intended). 顶部工具栏可见,但按钮不可见且不可点击,底部工具栏完全消失(第一个窗格“aDrawPane”按预期工作)。 I came across this problem when I wanted to put the view.fxml in a Tab. 当我想将view.fxml放在Tab中时,我遇到了这个问题。 Why is this happening and how can I make the toolbar and the buttons visible again? 为什么会发生这种情况,如何让工具栏和按钮再次可见?

The reason is that a Group does not resize its children, but renders them at their preferred size. 原因是一个集团没有调整其子项的大小,而是将它们设置为首选大小。

The FXML states that the Pane should be FXML表明Pane应该是

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

and that is exactly what you get. 而这正是你得到的。

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

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