"전용 GUI를 사용하여?"
난 당신이 디자인 뷰에서 의미 가정합니다. 나는 그렇게 생각하지 않는다. 그냥 코드를 작성하십시오. 그렇게 어렵지 않습니다.
여기 맵 값으로 Student
오브젝트 맵 key
로 Student
id
를 사용하는 예이다. key
은 JComboBox
의 표시 값입니다. 값은 get(id)
을 사용하여 선택 항목에서 검색됩니다.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MapCombo {
public MapCombo() {
Map<Integer, Student> map = createMap();
JComboBox cbox = createComboBox(map);
cbox.setBorder(new EmptyBorder(20, 20, 20, 20));
JFrame frame = new JFrame("Map ComboBox");
frame.add(cbox);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private Map<Integer, Student> createMap() {
Map<Integer, Student> map = new HashMap<>();
Student s1 = new Student(23, "Micheal Jordan");
Student s2 = new Student(6, "Lebron James");
Student s3 = new Student(3, "Chris Paul");
Student s4 = new Student(8, "Kobe Briant");
Student s5 = new Student(21, "Tim Duncan");
map.put(s1.getId(), s1);
map.put(s2.getId(), s2);
map.put(s3.getId(), s3);
map.put(s4.getId(), s4);
map.put(s5.getId(), s5);
return map;
}
private JComboBox createComboBox(final Map<Integer, Student> map) {
final JComboBox cbox = new JComboBox();
for (Integer id : map.keySet()) {
cbox.addItem(id);
}
cbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Integer id = (Integer)cbox.getSelectedItem();
System.out.println(map.get(id));
}
});
return cbox;
}
public class Student {
String name;
Integer id;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Name: " + name + " - Stud ID: " + id;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MapCombo();
}
});
}
}
당신이 달성하고자하는 바를 정확히 설명 할 수 있습니까? 대답은 예 또는 아니오 –
@peeskillet을 검토하십시오. 질문이 업데이트되었습니다. – hiddenuser
그러면 디스플레이가 핵심입니까? –