2017-12-24 54 views
0

私のアプリケーションでは、複数の.fxmlファイルがあります。アプリケーションで画面を設定するときには、SetOnCloseRequest()メソッドも設定します。いくつかの画面では、その方法のコードが別の画面に切り替えるだけです。例:Create Bankの画面が開いているときに閉じるボタンを押すと、Manage Banksの画面に戻りたいとします。しかし、閉じるボタンを押すと、画面が正しく切り替わるようですが、何らかの理由でManage Banks画面を閉じた直後に、アプリケーションの実行が停止します。私はGUIを持っていないので、それ以降は何もできません。 誰かがManage Banksの画面が閉じないようにする方法を知っていますか?JavaFX SetOnCloseRequest()すべての画面を閉じる

public class ScreensController extends StackPane { 

    private HashMap<String, Node> screens = new HashMap<>(); 
    private Client client; 
    private NewTransactionController newTransactionController; 
    private BankAccountController bankAccountController; 
    private ManageBanksController manageBanksController; 

    public Client getClient() { 
     return this.client; 
    } 

    public NewTransactionController getNewTransactionController() { 
     return this.newTransactionController; 
    } 

    public BankAccountController getBankAccountController() { 
     return this.bankAccountController; 
    } 

    public ManageBanksController getManageBanksController() { 
     return this.manageBanksController; 
    } 

    public ScreensController() { 
     try { 
      this.client = new Client(); 
      System.out.println("Client: Client created"); 
     } catch (RemoteException e) { 
      System.out.println("Client: Cannot create Client"); 
      System.out.println("Client: RemoteException: " + e.getMessage()); 
      System.exit(0); 
     } 
    } 

    public void addScreen(String name, Node screen) { 
     screens.put(name, screen); 
    } 

    public void loadScreen(String name, String resource) { 
     try { 
      FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource)); 
      Parent loadScreen = myLoader.load(); 
      IControllers myScreenController = myLoader.getController(); 
      if (myScreenController instanceof NewTransactionController) { 
       this.newTransactionController = (NewTransactionController) myScreenController; 
      } else if (myScreenController instanceof BankAccountController) { 
       this.bankAccountController = (BankAccountController) myScreenController; 
      } else if (myScreenController instanceof ManageBanksController) { 
       this.manageBanksController = (ManageBanksController) myScreenController; 
      } 
      myScreenController.setScreenParent(this); 
      addScreen(name, loadScreen); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void setScreen(final String name) { 
     if (screens.get(name) != null) { 
      if (!getChildren().isEmpty()) { 
       getChildren().remove(0); 
       getChildren().add(0, screens.get(name)); 
       ClientMain.setProperties(name); 
      } else { 
       //First time start up 
       getChildren().add(screens.get(name)); 
       ClientMain.setProperties(name); 
      } 
     } else { 
      System.out.println("Screen hasn't been loaded!!!"); 
     } 
    } 
} 

答えて

1

この

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

... 

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

YES試してみてください。

はここ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クラスです!ありがとう!今はすべて期待どおりに動作します! – Svenmarim

関連する問題