我在第一个场景上有一个简单的按钮,它应该导致第二个场景 . 当我点击按钮时,它会改变场景,但只显示一个空白屏幕 . 如果我关闭原始舞台,然后在其上显示新场景,它可以正常工作,但我不想打开一个新窗口 .
这是我的控制器类:
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);
}
}
1 回答
当我像这样切换场景时,它对我有用: