我通过以下方式加载舞台:
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();
}
文件格式:
<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();
}
文件格式:
<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>
ok,现在可以用fx:root发送一个物体,制作一个场景,但是还是有场景不适合舞台的问题
现在有一个新问题,我没有没有fx:root,这个是使用另一个AnchorPane来重叠当前的那个,现在不重叠它,它只是出现在中间但大小保持不变小(装锚前)