我在第一个场景上有一个简单的按钮,它应该通向第二个场景。当我点击按钮时,它会改变场景,但只显示一个空白屏幕。如果我关闭原始舞台,然后在其上显示新场景,它可以正常工作,但我不想打开一个新窗口。
这是我的控制器类:
public class TitleController implements Initializable {
@FXML
private Button startButton;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
//When next/start/main buttons are clicked, transition scene
public void transitionScenes() throws IOException{
Stage stage;
Stage newStage;
Parent root;
stage = (Stage) startButton.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("Round_1.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
主要课程:
public class Main extends Application{
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Title_Screen.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
当我切换场景时,它对我有用:
public class TestApp extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Button btnChangeScene = new Button("change scene");
Button btnChangeSceneBack = new Button("change sceneBack");
btnChangeScene.setOnAction(e -> {
StackPane root = new StackPane(btnChangeSceneBack);
root.setStyle("-fx-background-color: orange");
Scene scene = new Scene(root, 300, 300);
Stage stage = (Stage) btnChangeScene.getScene().getWindow();
stage.setScene(scene);
});
StackPane root = new StackPane(btnChangeScene);
Scene scene = new Scene(root, 300, 300);
btnChangeSceneBack.setOnAction(e -> {
primaryStage.setScene(scene);
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}