我需要有关如何将文本字段中的用户字符串输入存储到 ArrayList
内的 Appointment
)的帮助。我收到的错误是“无法将字符串转换为对象约会
”,我不知道如何将时间和约会存储到自定义类型对象中,然后将其存储到数组列表
。
我正在我的 ButtonListener
方法中尝试此操作
class appointment{
private String time;
private String appt;
public appointment()
{
time = " ";
appt = " ";
}
public appointment(String time, String appt)
{
this.time = time;
this.appt = appt;
}
public String getTime()
{
return this.time;
}
public String getAppt()
{
return this.appt;
}
public void setTime(String time1)
{
time1 = time;
}
public void setAppt(String appt1)
{
appt1 = appt;
}
}
private class ButtonListener implements EventHandler<ActionEvent>
{
private Button pressedButton = new Button();
List<appointment> str = new ArrayList<appointment>();
@Override
public void handle(ActionEvent e)
{
if(e.getSource() instanceof Button)
{
pressedButton = (Button) e.getSource();
if(pressedButton == submit)
{
appointment i = time1.getText();
appointment g = appt1.getText();
str.add(i, g);
JOptionPane.showMessageDialog(null, "Added");
}
}
}
}
}
最佳答案
您需要创建新的 Appointment 对象,然后插入到 str 中。
Appointment appointment = new Appointment(time1.getText(), appt1.getText());
str.add(appointment);
关于java - Java 新手,需要帮助将文本字段中的元素存储到自定义类型对象中并将该对象存储到 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61736798/