【发布时间】:2015-11-18 00:33:47
【问题描述】:
我能否将来自不同文本字段的文本存储在 ArrayList 中,并将其存储到 Booking 类中的 Customer 属性中。现在它正在读取每个文本字段并将它们保存到不同的属性中。
private String flighttime;
private String flightlocation;
private String flightfee;
private boolean car;
private boolean insurance;
private Customer customer;
private void savebookingButtonActionPerformed(java.awt.event.ActionEvent evt) {
Booking customerbooking = new Booking();
Customer cust = null;
try {
if (custnameTF.getText().equals("")) {
throw new EmptyField("Please Insert Customer");
} else {
FileOutputStream fos = new FileOutputStream("Bookings/" + custidTF.getText() + ".txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
cust.setPersonName((custnameTF.getText()));
cust.setPersonSurname((custsurnameTF.getText()));
cust.setPersonID((custidTF.getText()));
cust.setConsultantname(consnameTF.getText());
cust.setConsultantsurname((conssurnameTF.getText()));
cust.setConsulid(considTF.getText());
customerbooking.setFlightlocation(locationCB.getSelectedItem().toString());
customerbooking.setFlighttime(timeCB.getSelectedItem().toString());
customerbooking.setFlightfee(feeCB.getSelectedItem().toString());
customerbooking.setCar(carRB.isSelected());
customerbooking.setInsurance(insuranceRB.isSelected());
oos.writeObject(customerbooking);
oos.close();
fos.close();
custnameTF.setText("");
custsurnameTF.setText("");
custidTF.setText("");
considTF.setText("");
consnameTF.setText("");
conssurnameTF.setText("");
locationCB.setSelectedItem("");
timeCB.setSelectedItem("");
feeCB.setSelectedItem("");
【问题讨论】:
-
您宁愿寻找
HashMap而不是ArrayList以便能够更好地识别值。但是Booking类中的 6 个单变量有什么坏处呢? -
当然可以 - 但问题是:这样做是否明智?我会说不,因为您介绍了错误的来源并丢失了信息(例如字段名称等)。
-
Java 不是 Javascript。请为标签和语言标签使用正确的内容。
-
由于我有一段时间没有使用 Swing 我不是这里的专家,但是 IIRC 你可以使用 bean 自省将属性绑定到 ui 组件,即你可以为组件提供一个属性名称和一个 bean 和框架将处理两者之间的传输 - 您搜索的关键字将是“java 数据绑定”,也可能是“swing”或“javafx”。
标签: java arrays list class properties