我今天早些时候发布了一个问题,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没有。如有任何帮助,我们将不胜感激。干杯
使用自定义渲染器只是解决方案的一半。自定义渲染器将通过使用组合框的键盘来打破默认项目选择。有关详细信息和更完整的解决方案,请参见带有自定义渲染器的组合框。
代码的主要问题是NotWorking类。这个类不应该扩展JPanel。它只是一个用于保存类的3个属性的类。课程编号、标题和级别不应是静态变量。不应该引用Swing组件。
您的设计应该是NotWorking类的一个类和创建GUI的另一个类。
从Swing教程中关于如何使用组合框以获得更好的设计的部分开始。然后自定义教程中的ComboBoxDemo.java
示例代码,将NotWorking类添加到组合框中,而不是添加String数据。