【问题标题】:scene not fitting in the stage场景不适合舞台
【发布时间】:2013-02-18 14:46:55
【问题描述】:

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

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>

换场景的时候问题来了;新场景更小,出现在左上角并且不完全适合窗口(窗口保持其大小,调整窗口大小后,场景再次调整到窗口,但之前不是)。

另外,是否可以将对象发送到视图的控制器,因为我需要使用控制器来处理模型? (使用 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来重叠当前的,现在不重叠了,它只是出现在中间但是尺寸保持小(在安装锚之前)

【问题讨论】:

  • 你的例子对我不起作用(点击按钮没有任何作用)
  • 我建议不要更改场景,而是在要更改屏幕时替换其根。 FXML 的最顶层部分将是一个带有控制器类的布局容器。要将对象发送到控制器,首先在加载 FXML 后获取控制器实例,然后调用您在控制器中编写的方法来更新视图。
  • 可以给我一个替换root并向控制器发送对象的示例吗?,没有找到任何...(或搜索时使用的关键字不正确...)
  • 谢谢,第一个链接将有助于将对象发送到控制器。并且 fx:root 仍然不太了解它是如何工作的,将不得不搜索有关它的更多信息(或使用它更改场景的示例)

标签: java xml javafx-2 fxml


【解决方案1】:

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

@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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2018-07-05
    • 1970-01-01
    • 2020-07-02
    • 2018-04-08
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多