我有一个适用于FXML文件的应用程序。每次我想加载另一个FXML文件,我得到一个我的Controller类的新实例。但我不想那样。我该如何防止这种情况?
例如:我刚刚登录了我的login.fxml文件,现在需要关闭login.fxml文件并需要显示manageBanks.fxml文件。我编程的是它查看当前不是NULL的AnchorPane并从该窗格中获取窗口,因此我可以关闭该阶段。然后我加载我的控制器中的新.fxml文件。但每次我加载一个新的.fxml文件时,应用程序都会创建一个新的控制器实例。但是我在Controller类中有一个Client类的实例(在构造函数中设置),我不希望每次都更新该实例。我只想要一个控制器可以与之交谈的Client类的实例。
这是我的Controller类。在这个类的底部是我初始化新阶段的方法:
public class Controller {
public TextField tbIban;
public TextField tbFirstName;
public TextField tbLastName;
public TextField tbPostalCode;
public TextField tbHouseNumber;
public TextField tbEmail;
public TextField tbBankName;
public TextField tbShortcut;
public TextField tbEuroIn;
public TextField tbCentIn;
public TextField tbCentOut;
public TextField tbEuroOut;
public TextField tbEuroAmount;
public TextField tbCentAmount;
public TextField tbNameReceiver;
public TextField tbIbanReceiver;
public TextArea tbDescription;
public PasswordField tbPassword;
public PasswordField tbRepeatPassword;
public Label lbName;
public Label lbIban;
public Label lbAmount;
public TableView tabelTransactions;
public TableView tabelAddresses;
public TableView tabelBanks;
public DatePicker dtpDateOfBirth;
public CheckBox cbAddToAddressBook;
public ComboBox cmbbank;
public AnchorPane screenEditAccount;
public AnchorPane screenAddressBook;
public AnchorPane screenAddressBookTransaction;
public AnchorPane screenBankAccount;
public AnchorPane screenCreateBank;
public AnchorPane screenCreateBankAccount;
public AnchorPane screenLimits;
public AnchorPane screenLogin;
public AnchorPane screenManageBanks;
public AnchorPane screenNewTransaction;
private Client client;
public Controller() {
try {
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 login() {
String iban = tbIban.getText();
String password = tbPassword.getText();
try {
if (client.login(iban, password)){
if (iban.equals("admin")) {
changeScreenTo(Screens.MANAGEBANKS);
} else {
changeScreenTo(Screens.BANKACCOUNT);
}
} else {
showErrorMessage("Wrong username or password.");
}
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
}
public void logoutClient() {
try {
client.logout();
changeScreenTo(Screens.LOGIN);
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
}
public void logoutAdmin() {
try {
client.logout();
changeScreenTo(Screens.LOGIN);
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
}
public void cancelToLogin() {
changeScreenTo(Screens.LOGIN);
}
public void createBank() {
String name = tbBankName.getText();
String shortcut = tbShortcut.getText();
try {
if (client.createBank(name, shortcut)){
changeScreenTo(Screens.MANAGEBANKS);
} else {
showErrorMessage("Bank name or shortcut already excist.");
}
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
}
public void deleteBank() {
String bank = String.valueOf(tabelBanks.getSelectionModel().getSelectedItem());
String[] bankParts = bank.split(";");
String bankName = bankParts[0];
client.deleteBank(bankName);
}
public void createBankAccount() {
String password = tbPassword.getText();
String passwordRepeat = tbRepeatPassword.getText();
String firstName = tbFirstName.getText();
String lastName = tbLastName.getText();
String postalCode = tbPostalCode.getText();
int houseNumber = Integer.parseInt(tbHouseNumber.getText());
Date dateOfBirth = Date.from(dtpDateOfBirth.getValue().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
String email = tbEmail.getText();
String bankName = cmbbank.getValue().toString();
if (password.equals(passwordRepeat) && !password.equals("")){
if (!firstName.equals("") && !lastName.equals("") && !postalCode.equals("") && houseNumber != 0 && dateOfBirth.before(new Date()) && !email.equals("") && !bankName.equals("")){
try {
if (client.createBankAccount(bankName, password, firstName, lastName, postalCode, houseNumber, dateOfBirth, email)){
changeScreenTo(Screens.BANKACCOUNT);
} else {
showErrorMessage("Something went wrong with creating your account.");
}
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
} else {
showErrorMessage("Personal details are not valid or there is no bank selected.");
}
} else {
showErrorMessage("Password can not be empty or is not the same as repeated password.");
}
}
public void editBankAccount() {
String password = tbPassword.getText();
String passwordRepeat = tbRepeatPassword.getText();
String firstName = tbFirstName.getText();
String lastName = tbLastName.getText();
String postalCode = tbPostalCode.getText();
int houseNumber = Integer.parseInt(tbHouseNumber.getText());
Date dateOfBirth = Date.from(dtpDateOfBirth.getValue().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
String email = tbEmail.getText();
if (password.equals(passwordRepeat) && !password.equals("")){
if (!firstName.equals("") && !lastName.equals("") && !postalCode.equals("") && houseNumber != 0 && dateOfBirth.before(new Date()) && !email.equals("")){
try {
client.editBankAccount(password, firstName, lastName, postalCode, houseNumber, dateOfBirth, email);
changeScreenTo(Screens.BANKACCOUNT);
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
} else {
showErrorMessage("Personal details are not valid.");
}
} else {
showErrorMessage("Password can not be empty or is not the same as repeated password.");
}
}
public void deleteBankAccount() {
try {
client.deleteBankAccount();
changeScreenTo(Screens.LOGIN);
} catch (RemoteException e) {
showErrorMessage(e.getMessage());
}
}
public void cancelToBankAccount() {
changeScreenTo(Screens.BANKACCOUNT);
}
public void editBankAccountsLimits() {
double limitIn = Double.parseDouble(tbEuroIn.getText() + "," + tbCentIn.getText());
double limitOut = Double.parseDouble(tbEuroOut.getText() + "," + tbCentOut.getText());
if (limitIn > 0 && limitOut > 0){
client.editBankAccountsLimits(limitIn, limitOut);
changeScreenTo(Screens.BANKACCOUNT);
} else {
showErrorMessage("Limits can not be 0.");
}
}
public void deleteBankAccountsAddress() {
Address address = (Address) tabelAddresses.getSelectionModel().getSelectedItem();
client.deleteBankAccountsAddress(address);
}
public void makeBankAccountsTransaction() {
double amount = Double.parseDouble(tbEuroAmount.getText() + "," + tbCentAmount.getText());
String nameReceiver = tbNameReceiver.getText();
String ibanReceiver = tbIbanReceiver.getText();
String description = tbDescription.getText();
boolean addToAddress = cbAddToAddressBook.isSelected();
if (amount > 0 && !nameReceiver.equals("") && !ibanReceiver.equals("")){
client.makeBankAccountsTransaction(amount, nameReceiver, ibanReceiver, description, addToAddress);
changeScreenTo(Screens.BANKACCOUNT);
} else {
showErrorMessage("Amount and receiver details can not be empty.");
}
}
public void chooseAddress() {
Address address = (Address) tabelAddresses.getSelectionModel().getSelectedItem();
tbNameReceiver.setText(address.getName());
tbIbanReceiver.setText(address.getIban());
changeScreenTo(Screens.NEWTRANSACTION);
}
public void makeBankAccountsRequest() {
double amount = Double.parseDouble(tbEuroAmount.getText() + "," + tbCentAmount.getText());
String nameReceiver = tbNameReceiver.getText();
String ibanReceiver = tbIbanReceiver.getText();
String description = tbDescription.getText();
boolean addToAddress = cbAddToAddressBook.isSelected();
if (amount > 0 && !nameReceiver.equals("") && !ibanReceiver.equals("")){
client.makeBankAccountsRequest(amount, nameReceiver, ibanReceiver, description, addToAddress);
changeScreenTo(Screens.BANKACCOUNT);
} else {
showErrorMessage("Amount and receiver details can not be empty.");
}
}
public void openCreateAccount() {
changeScreenTo(Screens.CREATEBANKACCOUNT);
}
public void openAccount() {
changeScreenTo(Screens.ACCOUNT);
}
public void openLimits() {
changeScreenTo(Screens.LIMITS);
}
public void openAddressBook() {
changeScreenTo(Screens.ADDRESSBOOK);
}
public void openAddressBookTransaction() {
changeScreenTo(Screens.ADDRESSBOOKTRANSACTION);
}
public void openTransaction() {
changeScreenTo(Screens.NEWTRANSACTION);
}
public void openCreateBank() {
changeScreenTo(Screens.CREATEBANK);
}
private void initAccountScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/account.fxml"));
stage.setTitle("Account");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.BANKACCOUNT));
stage.show();
}
private void initAddressBookScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/addressBook.fxml"));
stage.setTitle("Address Book");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.BANKACCOUNT));
stage.show();
}
private void initAddressBookTransactionScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/addressBookTransaction.fxml"));
stage.setTitle("Address Book");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.NEWTRANSACTION));
stage.show();
}
private void initBankAccountScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/bankAccount.fxml"));
stage.setTitle("Bank Account");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> logoutClient());
stage.show();
}
private void initCreateBankScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/createBank.fxml"));
stage.setTitle("Create Bank");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.MANAGEBANKS));
stage.show();
}
private void initCreateBankAccountScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/createBankAccount.fxml"));
stage.setTitle("Create Bank Account");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.LOGIN));
stage.show();
}
private void initLimitsScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/limits.fxml"));
stage.setTitle("Limits");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.BANKACCOUNT));
stage.show();
}
private void initLoginScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/login.fxml"));
stage.setTitle("Login");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> System.exit(0));
stage.show();
}
private void initManageBanksScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/manageBanks.fxml"));
stage.setTitle("Manage Banks");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> logoutAdmin());
stage.show();
}
private void initnewTransactionScreen() throws IOException {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Screens/newTransaction.fxml"));
stage.setTitle("New Transaction");
stage.setResizable(false);
stage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
stage.setScene(new Scene(root));
stage.setOnCloseRequest(event -> changeScreenTo(Screens.BANKACCOUNT));
stage.show();
}
private void showErrorMessage(String message){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR!");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void changeScreenTo(Screens screen) {
if (screenEditAccount != null) {
Stage currentStage = (Stage) screenEditAccount.getScene().getWindow();
currentStage.close();
} else if (screenAddressBook != null) {
Stage currentStage = (Stage) screenAddressBook.getScene().getWindow();
currentStage.close();
} else if (screenAddressBookTransaction != null) {
Stage currentStage = (Stage) screenAddressBookTransaction.getScene().getWindow();
currentStage.close();
} else if (screenBankAccount != null) {
Stage currentStage = (Stage) screenBankAccount.getScene().getWindow();
currentStage.close();
} else if (screenCreateBank != null) {
Stage currentStage = (Stage) screenCreateBank.getScene().getWindow();
currentStage.close();
} else if (screenCreateBankAccount != null) {
Stage currentStage = (Stage) screenCreateBankAccount.getScene().getWindow();
currentStage.close();
} else if (screenLimits != null) {
Stage currentStage = (Stage) screenLimits.getScene().getWindow();
currentStage.close();
} else if (screenLogin != null) {
Stage currentStage = (Stage) screenLogin.getScene().getWindow();
currentStage.close();
} else if (screenManageBanks != null) {
Stage currentStage = (Stage) screenManageBanks.getScene().getWindow();
currentStage.close();
} else if (screenNewTransaction != null) {
Stage currentStage = (Stage) screenNewTransaction.getScene().getWindow();
currentStage.close();
}
try {
switch (screen) {
case ACCOUNT:
initAccountScreen();
break;
case ADDRESSBOOK:
initAddressBookScreen();
break;
case ADDRESSBOOKTRANSACTION:
initAddressBookTransactionScreen();
break;
case BANKACCOUNT:
initBankAccountScreen();
break;
case CREATEBANK:
initCreateBankScreen();
break;
case CREATEBANKACCOUNT:
initCreateBankAccountScreen();
break;
case LIMITS:
initLimitsScreen();
break;
case LOGIN:
initLoginScreen();
break;
case MANAGEBANKS:
initManageBanksScreen();
break;
case NEWTRANSACTION:
initnewTransactionScreen();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的主要课程:
public class ClientMain extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Screens/login.fxml"));
primaryStage.setTitle("Login");
primaryStage.setResizable(false);
primaryStage.getIcons().add(new Image("file:assets/ideal_logo.jpg"));
primaryStage.setScene(new Scene(root));
primaryStage.setOnCloseRequest(event -> System.exit(0));
primaryStage.show();
}
}
答案 0 :(得分:1)
每次加载FXML文件时,都会获得FXML文件中定义的UI元素的新实例。例如,您的FXML文件显然有大量<TextField>
个元素;对于每个<TextField>
元素,每次加载FXML时都会创建一个新的TextField
实例。
由于控制器通常具有对FXML文件中定义的UI元素的引用,和/或具有特定于这些元素的事件处理程序,因此控制器实例特定于实例从对FXMLLoader.load()
的特定调用加载的UI的em>。
因此,在从多个FXMLLoader.load()
调用加载的多个UI之间共享单个控制器实例完全没有意义。每个应该有一个不同的控制器实例,这是默认行为。