我为java创建了舞台和场景应用程序。该程序要求用户单击显示客户信息的按钮。然后在应用程序窗口中弹出一个对话框,其中包含Label
和TextField
个用户信息。然后用户单击“确定”并将客户信息存储到Person
类对象中。我试图让用户为不同的客户多次输入信息并将其存储到List
。我无法存储多个客户。这就是我到目前为止所做的:
package application;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
final CustomerInfoPane custInfo = new CustomerInfoPane();
primaryStage.setScene(new Scene(custInfo, 375, 250));
primaryStage.setTitle("Customer Information");
primaryStage.show();
}
public class CustomerInfoPane extends GridPane {
Label firstNameLabel = new Label("First Name: ");
Label lastNameLabel = new Label("Last Name: ");
Label emailLabel = new Label("Email: ");
Label phoneLabel = new Label("Phone Number: ");
TextField enterFirstName = new TextField();
TextField enterLastName = new TextField();
TextField enterEmail = new TextField();
TextField enterPhone = new TextField();
Button enterInfoButton = new Button("Enter your info");
Button okButton = new Button(" OK ");
Button cancelButton = new Button(" Cancel ");
CustomerInfoPane() {
setVgap(10);
setPadding(new Insets(10, 10, 10, 10));
add(enterInfoButton, 0, 0);
enterInfoButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
add(firstNameLabel, 0, 2);
add(lastNameLabel, 0, 3);
add(phoneLabel, 0, 4);
add(emailLabel, 0, 5);
add(enterFirstName, 10, 2);
add(enterLastName, 10, 3);
add(enterPhone, 10, 4);
add(enterEmail, 10, 5);
add(okButton, 0, 7);
add(cancelButton, 10, 7);
enterFirstName.setAlignment(Pos.CENTER_RIGHT);
enterLastName.setAlignment(Pos.CENTER_RIGHT);
enterPhone.setAlignment(Pos.CENTER_RIGHT);
enterEmail.setAlignment(Pos.CENTER_RIGHT);
}
});
okButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String firstName = enterFirstName.getText();
String lastName = enterLastName.getText();
String phoneNum = enterPhone.getText();
String email = enterEmail.getText();
Person person1 = new Person(firstName, lastName, phoneNum, email);
Person person2 = new Person(firstName, lastName, phoneNum, email);
List<Person> people = new ArrayList<Person>();
people.add(person1);
people.add(person2);
for (Person i : people) {
System.out.println(i); //when this prints it print same data twice
}
}
});
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
}
}
public static void main(String[] args) {
launch(args);
}
}
这是我的人员类:
public class Person {
private String firstName;
private String lastName;
private String phoneNum;
private String email;
public Person(String firstName, String lastName, String phoneNum, String email){
this.setFirstName(firstName);
this.setLastName(lastName);
this.setPhoneNum(phoneNum);
this.setEmail(email);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "First Name: " + firstName + "\nLast Name: " + lastName + "\nPhone Number: "
+ phoneNum + "\nEmail: " + email;
}
}
这是我的控制台输出面板的图片: