1 list.setCellRenderer(new MyRenderer(new int[] {2,3}, Color.RED)); //设置列表框的第2行和第3行文字颜色为红色 2 3 class MyRenderer extends DefaultListCellRenderer { 4 5 private Font font1; 6 private Font font2; 7 private Color rowcolor; 8 private int row; 9 private int[] rows; 10 11 public MyRenderer() { 12 this.font1 = getFont(); 13 this.font2 = font1.deriveFont((float) (font1.getSize() + 10)); 14 } 15 16 public MyRenderer(int row, Color color) { 17 this.rowcolor = color; 18 this.row = row; 19 } 20 21 public MyRenderer(int[] rows, Color color) { 22 this.rowcolor = color; 23 this.rows = rows; 24 } 25 26 public Component getListCellRendererComponent(JList list, Object value, 27 int index, boolean isSelected, boolean cellHasFocus) { 28 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 29 if (rows==null) { 30 if (index == row) { 31 setForeground(this.rowcolor); 32 setFont(getFont().deriveFont((float) (getFont().getSize() + 2))); 33 } 34 } else { 35 for (int i = 0; i < rows.length; i++) { 36 if (index == rows[i]) { 37 setForeground(this.rowcolor); 38 setFont(getFont().deriveFont((float) (getFont().getSize() + 2))); 39 } 40 } 41 } 42 43 return this; 44 } 45 }