gpt4 book ai didi

Java 将对象转换为类型?

转载 作者:行者123 更新时间:2023-12-01 18:52:52 25 4
gpt4 key购买 nike

我想选择 JList 中特定于患者已单击内容的项目。如果我单击第三个患者,JList 将选择患者选择的项目。

PatientConList 中是该患者的病情对象。

当您添加患者时,您将执行以下操作:

List PatientConditions1 = new ArrayList();
PatientConditions1 = ConditionsJList.getSelectedValuesList();

然后,当您单击患者时,它会执行此操作

public void MakePatientJListSelected(ArrayList PatientConList) {
//this makes selected, so get condition array off patient.
//so call this from patient jlist listener
ConditionsJList.clearSelection();
//get number - 1, so then 0 = first
ArrayList<Conditions> PassedIn = PatientConList;
ArrayList SelectList = new ArrayList();

for(Conditions p: PassedIn)
{
int Num = p.getNumber();
Num = Num - 1;
SelectList.add(Num);
}

//int startSel = 0;
//int endSel = 0;
//ConditionsJList.setSelectionInterval(startSel, endSel);

for(int k=0;k<PassedIn.size();k++)
{
int startSel2 = k;
int endSel2 = k;
ConditionsJList.addSelectionInterval(startSel2, endSel2);
}
}

抱歉,基本上我有一个 JList,您可以选择您想要的条件并单击添加。这会向患者添加条件列表。

基本上,当您选择要查看的患者时,我需要它,以便在 jlist 上选择该患者的 jlist 条件。

我收到此错误线程“AWT-EventQueue-0”java.lang.ClassCastException 中的异常:java.lang.String 无法转换为 DentistAppNew.Conditions

我的病人类别:

import java.util.*;

public class Patient {

private int AccountNumber;
private String Name;
private int Age;
private String Address;
private String Sex;
private String Phone;
private List<Conditions> ConditionNames;

public Patient( int AccountNumber1,
String Name1,
int Age1,
String Address1,
String Sex1,
String Phone1,
List<Conditions> ConditionNames1)
{
this.AccountNumber = AccountNumber1;
this.Name = Name1;
this.Age = Age1;
this.Address = Address1;
this.Sex = Sex1;
this.Phone = Phone1;
this.ConditionNames = ConditionNames1
}

public int getAccountNumber() { return AccountNumber; }

public String getName() { return Name; }

public int getAge() { return Age; }

public String getAddress() { return Address; }

public String getSex() { return Sex; }

public String getPhone() { return Phone; }

public List getConditionsList() { return ConditionNames; }

public void setName(String Name1) { this.Name = Name1; }

public void setAge(int Age1) { this.Age = Age1; }

public void setAddress(String Address1) { this.Address = Address1; }

public void setSex(String Sex1) { this.Sex = Sex1; }

public void setPhone(String Phone1) { this.Phone = Phone1; }

}

我的条件类:

public class Conditions {

private int Number;
private String Name;

public Conditions( int Number1,String Name1) {
this.Number = Number1;
this.Name = Name1;
}

public int getNumber() {
return Number;
}

public String getName() {
return Name;
}
}

最佳答案

我怀疑您传递的列表是字符串列表,而不是您在此处假设的条件列表:

for(Conditions p: PassedIn)

但是,您给我们提供的内容很少。完整的错误消息是什么?

另外,尝试更改 MakePatientJListSelected 的类型至ArrayList<Condition> 。我想你会发现编译器会告诉你它需要 ArrayList<String>那里。

关于Java 将对象转换为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15179584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com