Java 不适合舞台的场景
我以以下方式加载阶段:Java 不适合舞台的场景,java,xml,javafx-2,fxml,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.setHei
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 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:
Fxml:
重叠:
@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:根,这个是另一个锚现在重叠的一个,现在不重叠,它只是出现在中间,但是在锚定之前尺寸保持小,
> P>找到适合场景的方式:
@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);
}
你的例子不适用于我点击按钮没有任何作用我建议不要改变场景,只要屏幕要改变,就替换它的根。FXMLs的最顶层将是一个带有控制器类的布局容器。若要将对象发送到控制器,请先在加载FXML后获取控制器实例,然后调用您在控制器中编写的方法来更新视图。请给我一个替换根并将对象发送到控制器的示例,但未找到任何。。。或者搜索中使用的关键字不正确…请参见,以及此处的相关Q&A。谢谢,第一个链接将有助于将对象发送到控制器。而且fx:root仍然不了解它是如何工作的,将不得不搜索更多关于它的信息或一个关于使用它改变场景的示例
@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);
}
<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>
@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);
}