我今天早些时候发布了一个问题,并被 MadProgrammer 指示使用 ListCellRenderer 来达到预期的结果。我几乎可以使用它,但是它在组合框中两次显示相同的条目,我不知道为什么。请帮我解开这个谜。编码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NotWorking extends JPanel {
private JPanel editPanel;
private JComboBox<String> editComboLevel;
private JComboBox editCombo;
private String[] levels = {"Level 1", "Level 2", "Level 3"};
private static ArrayList<NotWorking> course = new ArrayList<NotWorking>();
public static String courseNum, courseTitle, courseLevel;
public JPanel createContentPane (){
Integer[] intArray = new Integer[course.size()];
for (int i = 0; i < course.size(); i++) {
intArray[i] = new Integer(i);
}
editPanel = new JPanel(new GridLayout(4,2));
editPanel.setPreferredSize(new Dimension(100,75));
editPanel.add(editCombo = new JComboBox(intArray));
ComboBoxRenderer renderer= new ComboBoxRenderer();
editCombo.setRenderer(renderer);
return editPanel;
}
NotWorking() {}
NotWorking(String courseNum, String courseTitle, String courseLevel) {
this.courseNum = courseNum;
this.courseTitle = courseTitle;
this.courseLevel = courseLevel;
}
@Override
public String toString() {
String courseInfo = getCourseNum() + ", " + getCourseTitle() + ", " + getCourseLevel();
return courseInfo;
}
public String getCourseNum() {
return this.courseNum;
}
public String getCourseTitle() {
return this.courseTitle;
}
public String getCourseLevel() {
return this.courseLevel;
}
public void setCourseNum(String courseNum) {
this.courseNum = courseNum;
}
public void setCourseTitle(String courseTitle) {
this.courseTitle = courseTitle;
}
public void setCourseLevel(String courseLevel) {
this.courseLevel = courseLevel;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Example of Code Snippet");
NotWorking myCourse = new NotWorking();
frame.setContentPane(myCourse.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
course.add(new NotWorking("Course1", "Course1 desc", "Level 1"));
course.add(new NotWorking("Course2", "Course2 desc", "Level 2"));
createAndShowGUI();
for(NotWorking item : course)
System.out.println(item);
}
});
}
class ComboBoxRenderer extends JLabel
implements ListCellRenderer {
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
int selectedIndex = ((Integer)value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setText(getCourseNum());
return this;
}
}
}
如您所见,ArrayList 有 2 个添加项。我将组合框中的显示限制为仅显示课程编号,但 Course2 显示了两次,当我打印出 ArrayList 的内容时,我看到 Course2 的所有详细信息显示了两次,而 Course1 则没有。任何帮助将不胜感激。干杯