gpt4 book ai didi

JavaFX SetOnCloseRequest() 关闭所有屏幕

转载 作者:行者123 更新时间:2023-11-30 06:21:57 25 4
gpt4 key购买 nike

在我的应用程序中,我有多个 .fxml 文件。当我在应用程序中设置屏幕时,我还设置了 SetOnCloseRequest() 方法。对于某些屏幕,该方法中的代码只是为了切换到另一个屏幕。例如:当我打开创建银行屏幕并按下关闭按钮时,我想切换回管理银行屏幕。但是,当我按下关闭按钮时,它似乎可以正确切换屏幕,但在它因某种原因关闭管理银行屏幕后不久,应用程序不会停止运行。所以之后我什么也做不了,因为我没有任何 GUI。有人知道如何确保管理银行屏幕不会关闭吗?

这是我的主类,其中包含 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 类:

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!!!");
}
}
}

最佳答案

试试这个

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

...

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

关于JavaFX SetOnCloseRequest() 关闭所有屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com