我编写了自己的 ListCellRenderer(下面的代码),它使我的单元格透明。现在如果我运行这个,每次我点击一个列表项,它都会变得比以前更白。会不会是,JList 总是添加这个新的 JLabel - 我创建的 - 并覆盖最后一个?
jListPriority.setOpaque(true);
jListPriority.setBackground(new Color(255, 255, 255, 10));
jListPriority.setBorder(BorderFactory.createLineBorder(new Color(250, 250, 250)));
jListPriority.setCellRenderer(new ListCellRenderer() {
private JLabel label = new JLabel();
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
label.setOpaque(true);
label.setText(value.toString());
label.setForeground(new Color(250, 250, 250));
label.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
label.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(250, 250, 250, 50)));
label.setPreferredSize(new Dimension(label.getPreferredSize().width, label.getPreferredSize().height + 3));
if (isSelected) {
label.setBackground(new Color(255, 255, 255, 20));
} else {
label.setBackground(new Color(255, 255, 255, 10));
}
return label;
}
});