我按以下方式加载舞台:
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,并具有以下内容:
Aplication:
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重叠当前的一个,现在不重叠它,它只是出现在中间但是尺寸保持较小(在安装锚之前)
答案 0 :(得分:0)
找到适合舞台的方式:
@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);
}