java - 场景不适合舞台

标签 java xml javafx-2 fxml

我通过以下方式加载舞台:

public void start(Stage stage) throws Exception{
    stage.setTitle("title");
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml"));
    stage.setScene(scene);
    stage.setWidth(1080);
    stage.setHeight(720);
    stage.setFullScreen(false);
    stage.show();
}

更改场景:

@FXML protected void click(ActionEvent event) throws Exception{
    Stage stage = (Stage)menu.getScene().getWindow();
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml"));
    stage.setScene(scene);
    stage.show();
}

Fxml:

<Scene fx:controller="controller.CtrlMainMenu" xmlns:fx="http://javafx.com/fxml" stylesheets="view/Style.css">
    <AnchorPane fx:id="menu">
        <VBox spacing="8"
              AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
              AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
            <Button text="click" onAction="#click"/>
        </VBox>
    </AnchorPane>
</Scene>

切换场景时就会出现问题;新场景较小,出现在左上角,并且不完全适合窗口(窗口保持其大小,调整窗口大小后,场景再次重新适合窗口,但之前不是)。

此外,是否可以将对象发送到 View 的 Controller ,因为我需要使用 Controller 来处理模型? (采用MVC架构)

<小时/>

现在使用 fx:root 尝试此方法 2,结果如下:

应用:

public void start(Stage stage) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/MainMenu.fxml").openStream());
    CtrlMainMenu controller = (CtrlMainMenu) fxmlLoader.getController();
    controller.sendString("test");      //send object test
    stage.setScene(new Scene(p));

    stage.setWidth(1080);
    stage.setHeight(720);
    stage.setFullScreen(false);
    stage.show();
}

Fxml:

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.CtrlMainMenu">
    <children>
        <AnchorPane fx:id="menu">
            <VBox spacing="8"
                  AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0"
                  AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                <Button text="click" onAction="#click"/>
            </VBox>
        </AnchorPane>
    </children>
</fx:root>

更改场景:

@FXML protected void click(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    Stage stage = (Stage)menu.getScene().getWindow();
    stage.setScene(new Scene(p));
    stage.show();
}

重叠:

@FXML protected void Options(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    menu.getChildren().add(p);
}

fxml(旧的没有 fx:root 行):

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml"
         fx:controller="controller.CtrlOptions" stylesheets="view/Style.css">
    <children>
        <GridPane xmlns:fx="http://javafx.com/fxml" alignment="center"
                  hgap="10" vgap="10" id="optionBackgorund"
                  AnchorPane.topAnchor="50.0" AnchorPane.bottomAnchor="50.0"
                  AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0"
                  fx:id="options">
            <!-- the view -->
        </GridPane>
    </children>
</fx:root>

好的,现在可以发送对象并使用 fx:root 制作场景,但仍然存在场景不适合舞台的问题

现在有一个新问题,没有 fx:root 就没有,这个问题是使用另一个 AnchorPane 来重叠当前的,现在不重叠它,它只出现在中间,但是尺寸保持较小(安装 anchor 之前)

最佳答案

找到了使场景适合舞台的方法:

@FXML protected void click(ActionEvent event) throws Exception{
    FXMLLoader fxmlLoader = new FXMLLoader();
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream());
    Scene scene= menu.getScene();
    scene.setRoot(p);
}

关于java - 场景不适合舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14938945/

相关文章:

java - 单独的流口水规则还是有更多的结果?

Java - 如何在网格布局中平滑地将图像从一个点移动到另一个点?

android - 重命名变量以使 XML id 更清晰

javafx-2 - TextArea - 是否可以获得行数?

java - 无法在 javafx 中将数组列表作为参数从一个窗口传递到另一个窗口

java - Android,如何更改状态更改时的操作栏 Logo 按钮?

xml - XPath 查找分割为多个后代的文本

html - 关闭斜线之前的空间?

css - 如何为单个系列条形图添加 3 个图例?? JAVAFX

java - 处理 'not' 条件作为流操作中的方法引用