从弹出 window JavaFx 中获取 return 类型的最佳方法
best way to get return type from a popup window JavaFx
从弹出窗口 window 中获取 return 类型的最佳方法是什么。我正在创建一个弹出窗口 window,其中包含需要填写的字段。我想从这些字段中获取 return 类型的 Person。
这里是我调用弹出窗口的地方 window。
Button addButton = new Button("Add Person to interested list");
AddPersonAdop adp = new AddPersonAdop();
addButton.setOnAction(e -> table.getItems().add(adp.addButtonPerson()));
这是弹出窗口 window class
public class AddPersonAdop {
private TextField personNameAdoption, personAddressAdoption, personNoAdoption, personEmailAdoption;
public Person display() {
final Stage window = new Stage();
BorderPane bP = new BorderPane();
window.setTitle("Add Person");
Button addPerson = new Button("Add Selected Animal");
addPerson.setOnAction(e -> {
window.close();
addButtonPerson();});
Button closeButton = new Button("Close Window");
closeButton.setOnAction(e -> window.close());
VBox vbox = new VBox(personFieldsAdoption(), addPerson, closeButton);
vbox.getChildren().addAll();
bP.setCenter(vbox);
Scene scene = new Scene(bP, 800,800);
window.setScene(scene);
window.show();
return addButtonPerson();
}
public GridPane personFieldsAdoption(){
personNameAdoption = new TextField();
personNameAdoption.setPromptText("Name");
personNameAdoption.setMinWidth(75);
personAddressAdoption = new TextField();
personAddressAdoption.setPromptText("Address");
personAddressAdoption.setMinWidth(75);
personNoAdoption = new TextField();
personNoAdoption.setPromptText("Phone Number");
personNoAdoption.setMinWidth(75);
personEmailAdoption = new TextField();
personEmailAdoption.setPromptText("Email");
personEmailAdoption.setMinWidth(75);
GridPane grid = new GridPane();
grid.centerShapeProperty();
grid.setHgap(10);
grid.setPadding(new Insets(10,10,10,10));
grid.addRow(0, CustomItems.label("Enter Persons Details"));
grid.addRow(1, new Label("Person Name *"), new Label(" Address *"), new Label("Phone Number *"), new Label("Email *"));
grid.addRow(2, personNameAdoption, personAddressAdoption, personNoAdoption, personEmailAdoption);
return grid;
}
public Person addButtonPerson(){
if(personNameAdoption.getText().isEmpty() || personAddressAdoption.getText().isEmpty() || personEmailAdoption.getText().isEmpty() || personNoAdoption.getText().isEmpty())
{
AlertBox.warn("Some Person Info Missing", "Please complete all Person mandatory fields");
Person person = new Person("name","add","@", "999");
return person;
}
else{
Person person = new Person(personNameAdoption.getText(),personAddressAdoption.getText(),personEmailAdoption.getText(), personNoAdoption.getText());
return person;
}
}
}
在 display()
方法中使用 window.showAndWait()
而不是 window.show()
。
然后
addButton.setOnAction(e -> table.getItems().add(adp.display()));
从弹出窗口 window 中获取 return 类型的最佳方法是什么。我正在创建一个弹出窗口 window,其中包含需要填写的字段。我想从这些字段中获取 return 类型的 Person。
这里是我调用弹出窗口的地方 window。
Button addButton = new Button("Add Person to interested list");
AddPersonAdop adp = new AddPersonAdop();
addButton.setOnAction(e -> table.getItems().add(adp.addButtonPerson()));
这是弹出窗口 window class
public class AddPersonAdop {
private TextField personNameAdoption, personAddressAdoption, personNoAdoption, personEmailAdoption;
public Person display() {
final Stage window = new Stage();
BorderPane bP = new BorderPane();
window.setTitle("Add Person");
Button addPerson = new Button("Add Selected Animal");
addPerson.setOnAction(e -> {
window.close();
addButtonPerson();});
Button closeButton = new Button("Close Window");
closeButton.setOnAction(e -> window.close());
VBox vbox = new VBox(personFieldsAdoption(), addPerson, closeButton);
vbox.getChildren().addAll();
bP.setCenter(vbox);
Scene scene = new Scene(bP, 800,800);
window.setScene(scene);
window.show();
return addButtonPerson();
}
public GridPane personFieldsAdoption(){
personNameAdoption = new TextField();
personNameAdoption.setPromptText("Name");
personNameAdoption.setMinWidth(75);
personAddressAdoption = new TextField();
personAddressAdoption.setPromptText("Address");
personAddressAdoption.setMinWidth(75);
personNoAdoption = new TextField();
personNoAdoption.setPromptText("Phone Number");
personNoAdoption.setMinWidth(75);
personEmailAdoption = new TextField();
personEmailAdoption.setPromptText("Email");
personEmailAdoption.setMinWidth(75);
GridPane grid = new GridPane();
grid.centerShapeProperty();
grid.setHgap(10);
grid.setPadding(new Insets(10,10,10,10));
grid.addRow(0, CustomItems.label("Enter Persons Details"));
grid.addRow(1, new Label("Person Name *"), new Label(" Address *"), new Label("Phone Number *"), new Label("Email *"));
grid.addRow(2, personNameAdoption, personAddressAdoption, personNoAdoption, personEmailAdoption);
return grid;
}
public Person addButtonPerson(){
if(personNameAdoption.getText().isEmpty() || personAddressAdoption.getText().isEmpty() || personEmailAdoption.getText().isEmpty() || personNoAdoption.getText().isEmpty())
{
AlertBox.warn("Some Person Info Missing", "Please complete all Person mandatory fields");
Person person = new Person("name","add","@", "999");
return person;
}
else{
Person person = new Person(personNameAdoption.getText(),personAddressAdoption.getText(),personEmailAdoption.getText(), personNoAdoption.getText());
return person;
}
}
}
在 display()
方法中使用 window.showAndWait()
而不是 window.show()
。
然后
addButton.setOnAction(e -> table.getItems().add(adp.display()));