JavaFX SetOnCloseRequest()关闭所有屏幕

时间:2017-12-24 11:13:41

标签: java javafx

在我的应用程序中,我有多个.fxml文件。当我在应用程序中设置屏幕时,我还设置了import logo from './logo.svg'; function Logo() { return <object data={logo} aria-label="logo" type="image/svg+xml" style={{ height: '8em', paddingTop: '1em' }} />; } 方法。对于某些屏幕,该方法中的代码只是为了切换到另一个屏幕。例如:当我打开SetOnCloseRequest()屏幕并按下关闭按钮时,我想切换回Create Bank屏幕。但是当我按下关闭按钮时,它似乎正确地切换了屏幕,但是在它因某种原因关闭Manage Banks屏幕后不久,应用程序就不再停止运行了。所以在那之后我什么都做不了,因为我没有任何GUI。 有人知道如何确保Manage Banks屏幕没有关闭吗?

以下是我使用Manage Banks方法的主要课程:

SetOnCloseRequest()

这是我的public class ClientMain extends Application { public static String screenCreateBankId = "createBank"; public static String screenCreateBankFile = "Screens/createBank.fxml"; public static String screenCreateBankAccountId = "createBankAccount"; public static String screenCreateBankAccountFile = "Screens/createBankAccount.fxml"; public static String screenLoginId = "login"; public static String screenLoginFile = "Screens/login.fxml"; public static String screenManageBanksId = "manageBanks"; public static String screenManageBanksFile = "Screens/manageBanks.fxml"; private static Stage primaryStage; private static ScreensController mainContainer; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { ClientMain.primaryStage = primaryStage; mainContainer = new ScreensController(); mainContainer.loadScreen(ClientMain.screenCreateBankId, ClientMain.screenCreateBankFile); mainContainer.loadScreen(ClientMain.screenLoginId, ClientMain.screenLoginFile); mainContainer.loadScreen(ClientMain.screenManageBanksId, ClientMain.screenManageBanksFile); mainContainer.setScreen(ClientMain.screenLoginId); Group root = new Group(); root.getChildren().addAll(mainContainer); primaryStage.setResizable(false); primaryStage.getIcons().add(new Image("file:assets/ideal_logo.jpg")); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void setProperties(String name){ primaryStage.sizeToScene(); primaryStage.centerOnScreen(); switch (name){ case "createBank": primaryStage.setTitle("Create Bank"); primaryStage.setOnCloseRequest(event -> mainContainer.setScreen(screenManageBanksId)); break; case "login": primaryStage.setTitle("Login"); primaryStage.setOnCloseRequest(event -> System.exit(0)); break; case "manageBanks": primaryStage.setTitle("Manage Banks"); primaryStage.setOnCloseRequest(event -> mainContainer.getManageBanksController().logoutAdmin()); break; } } } 课程:

ScreensController

1 个答案:

答案 0 :(得分:1)

试试这个

primaryStage.setOnCloseRequest(event -> {
    event.consume();
    mainContainer.setScreen(screenManageBanksId);
});

...

primaryStage.setOnCloseRequest(event -> {
    event.consume();
    mainContainer.getManageBanksController().logoutAdmin();
});