我的Probem是我想要將我的地圖的鍵顯示爲JComboBox,並且它只顯示其中一個,這些代碼只是我遇到問題的部分,如果讀者不在代碼中看到問題,我也會在這裏列出其他類。所以基本上我有我的鍵作爲一個Instuctor對象和值作爲一組Student對象(一個教師有不止一個學生),當我創建JComboBox它只顯示其中一個鍵。我嘗試了不同的順序,因爲我想也許它只是使用最後一個鍵添加(如鍵被取代),但這不是我可以看到的問題。無論如何,這是越來越冗長,所以這裏是我的代碼。使用JComboBox和TreeMap時遇到問題
//Test Objects
TreeSet<Student> inst1Student = new TreeSet<Student>();
TreeSet<Student> inst2Student = new TreeSet<Student>();
TreeSet<Student> inst3Student = new TreeSet<Student>();
inst1Student.add(new Student("Jane Doe"));
inst2Student.add(new Student("Jhon Smith"));
inst3Student.add(new Student("Students Name"));
Instructor inst1 = new Instructor("Instructors Name1", inst1Student);
Instructor inst2 = new Instructor("Instructors Name3", inst2Student);
Instructor inst3 = new Instructor("Instructors Name3", inst3Student);
theList.put(inst1, inst1.getStudents());
theList.put(inst3, inst3.getStudents());
theList.put(inst2, inst2.getStudents());
//Make combo box
instructors = new JComboBox<>();
getInstructorsArrayList();
for (int i = 0; i < theInstructors.size(); i++){
System.out.println(theInstructors.get(i));
instructors.addItem(theInstructors.get(i));
}
panel3.add(instructors);
instructors.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You Clicked Someone");
}
});
並引用這是我有的getInstructorsArrayList()方法。
public static void getInstructorsArrayList() {
for (Entry<Instructor, Set<Student>> entry : theList.entrySet()) {
if (entry.getKey() != null) {
theInstructors.add(entry.getKey().getName());
}
}
}
教師班級:
package psl_Tracker;
import java.util.Set;
public class Instructor implements Comparable<Instructor> {
private static String name = null;
private static Set<Student> students = null;
public Instructor(String name, Set<Student> students) {
Instructor.name = name;
if (students != null) {
Instructor.students = students;
}
}
public String getName() {
return name;
}
public Set<Student> getStudents() {
return students;
}
@Override
public int compareTo(Instructor other) {
return getName().compareTo(other.getName());
}
}
並再次任何幫助,將不勝感激,我感受到了它,我只是沒有看到一點點失誤。任何人都看到的語法錯誤請告訴我(它是我的寵物)。
顯示你'Instructor'類的代碼,我想在這個類的例外。如果被覆蓋,我需要看到'compareTo()'實現和'hashCode()'方法。 – alex2410
哪裏是'theInstructors' –
theInstructors是一個ArrayList –
Brent