2017-12-24 13 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