我试图将客户的 ArrayList 传递回第一个场景,因为我想在表视图中显示客户。我将客户添加到第二场景控制器内的银行对象。我能够让它运行,唯一的问题是我必须创建一个 Bank 对象的新实例。我不确定我会在哪里实例化 Bank 对象,这样每次创建帐户并返回主屏幕时它都不会被重置
公共类 MainRun 扩展应用程序
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/sample/mainMenu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add("my_style.css");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
这是我的第一堂场景课
公共类 FirstSceneController 实现 Initializable
private Scene scene;
private Stage stage;
private Parent root;
@FXML
private MenuItem exit_menuItem;
@FXML
MenuBar menu_Bar;
@FXML
private Button createAccount;
@FXML
private GridPane gridPane_root;
@FXML
private StackPane parentContainer;
public void closeWindow(ActionEvent actionEvent) {
stage = (Stage) menu_Bar.getScene().getWindow();
stage.close();
}
public void switchToCreateAccountWindow(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("addAccount.fxml"));
scene = createAccount.getScene();
root.translateYProperty().set(scene.getHeight());
parentContainer.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(event1 -> {
parentContainer.getChildren().remove(gridPane_root);
});
timeline.play();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ImageView exitImage = new ImageView(new Image("exit_icon.jpg"));
exitImage.setSmooth(true);
exitImage.setPreserveRatio(true);
exitImage.setFitHeight(16);
exitImage.setFitWidth(16);
exit_menuItem.setGraphic(exitImage);
}
第二场景类
公共类 SecondSceneController 实现 Initializable
ObservableList<String> list = FXCollections.observableArrayList("Checking", "Savings");
private Parent root;
private Scene scene;
private Stage stage;
private StackPane parentContainer;
@FXML
private GridPane gridPane_root;
@FXML
private MenuBar menu_Bar;
@FXML
ComboBox<String> account_types;
@FXML
private Button cancel_button;
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField socialSecurityField;
@FXML
private TextField depositField;
@FXML
MenuItem exit_menuItem;
private double amount = 0.0;
Account account = null;
private Bank bank;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
account_types.setItems(list);
ImageView exitImage = new ImageView(new Image("exit_icon.jpg"));
exitImage.setSmooth(true);
exitImage.setPreserveRatio(true);
exitImage.setFitHeight(16);
exitImage.setFitWidth(16);
exit_menuItem.setGraphic(exitImage);
}
public void goBackToMainScreen() throws IOException {
root = FXMLLoader.load(getClass().getResource("mainMenu.fxml"));
scene = cancel_button.getScene();
root.translateXProperty().set(scene.getWidth());
parentContainer = (StackPane) scene.getRoot();
parentContainer.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateXProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(event1 -> {
parentContainer.getChildren().remove(gridPane_root);
});
timeline.play();
}
public void createAccountActionPerformed(ActionEvent event) throws IOException {
boolean isValid;
StringBuilder stringBuilder = new StringBuilder();
String firstName = "", lastName = "", ssn = "";
if (firstNameField.getText().isEmpty()) {
System.out.println("First name must not be empty");
} else {
firstName = firstNameField.getText();
}
if (lastNameField.getText().isEmpty()) {
System.out.println("Last name must not be empty");
} else {
lastName = lastNameField.getText();
}
if (socialSecurityField.getText().isEmpty()) {
System.out.println("SSN must not be empty");
}
if (!socialSecurityField.getText().matches("\\d{3}-\\d{2}-\\d{4}")) {
System.out.println("Invalid SSN");
} else {
ssn = socialSecurityField.getText().replace("-", "");
}
if (depositField.getText().isEmpty()) {
System.out.println("Enter an amount!");
isValid = false;
} else {
try {
amount = round(Double.parseDouble(depositField.getText()), 2);
isValid = true;
} catch (NumberFormatException e) {
System.out.println("Deposit must be a number!");
isValid = false;
}
}
if (account_types.getSelectionModel().isEmpty()) {
System.out.println("Please select an account type!");
isValid = false;
} else if (account_types.getSelectionModel().equals("Checking")) {
account = new Checking(amount);
isValid = true;
} else if (account_types.getSelectionModel().equals("Savings")) {
account = new Savings(amount);
isValid = true;
}
Customer customer = new Customer(firstName, lastName, ssn, account);
bank.addCustomer(customer);
if (isValid) {
for (Customer c : bank.getCustomers()) {
System.out.println(c.firstName + " " + c.lastName);
}
goBackToMainScreen();
}
}
public void closeWindow(ActionEvent event) {
stage = (Stage) menu_Bar.getScene().getWindow();
stage.close();
}
private static double round(double number, int places) {
if (places < 0) {
throw new IllegalArgumentException();
}
BigDecimal bd = new BigDecimal(number);
bd = bd.setScale(places, RoundingMode.FLOOR);
return bd.doubleValue();
}
持有客户数组列表的银行类 公共类银行
List<Customer> customers;
public Bank() {
customers = new ArrayList<>();
}
public void addCustomer(Customer customer) {
customers.add(customer);
}
public List<Customer> getCustomers() {
return customers;
}