加载新场景时如何保留工具栏?
到目前为止,这是我的代码:
Main.class
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Main.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<GridPane fx:controller="main.java.Controller"
xmlns:fx="http://javafx.com/fxml">
<ToolBar prefHeight="40.0" prefWidth="400.0">
<Button text="testButton" onAction="#testButton"/>
</ToolBar>
</GridPane>
Controller.class
我认为这个问题就在这里。我如何生成下一个场景有点破碎。
public class Controller {
@FXML
private void testButton1() throws IOException {
Parent window1;
try {
window1 = FXMLLoader.load(getClass().getResource("src/testButton1.fxml"));
Stage window1Stage;
Scene window1Scene = new Scene(window1, 400, 400);
window1Stage = Main.main();
window1Stage.setScene(window1Scene);
} catch (IOException e) {
e.printStackTrace();
}
}
}