给你JList的你参考一下 public class Test { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(200, 200); ArrayList fonts = new ArrayList(); final int SIZE = 24; Font font =new Font("Serif", Font.PLAIN, SIZE);
final JList fontList = new JList(fonts.toArray()); fontList.setVisibleRowCount(4); fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fontList.setCellRenderer(new FontCellRenderer()); JScrollPane scrollPane = new JScrollPane(fontList); JPanel p = new JPanel(); p.add(scrollPane); f.add(p); f.setVisible(true);
fontList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { Font font = (Font) fontList.getSelectedValue(); fontList.setFont(font); } }); } }class FontCellRenderer extends JLabel implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Font font = (Font) value; setText(font.getFamily()); setFont(font); setOpaque(true); setBackground(isSelected ? list.getSelectionBackground() : list .getBackground()); setForeground((index%2==0)?Color.red:Color.blue); //这个是你所要的 return this; } }
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200); ArrayList fonts = new ArrayList();
final int SIZE = 24;
Font font =new Font("Serif", Font.PLAIN, SIZE);
fonts.add(new Font("Serif", Font.PLAIN, SIZE));
fonts.add(new Font("SansSerif", Font.PLAIN, SIZE));
fonts.add(new Font("Monospaced", Font.PLAIN, SIZE));
fonts.add(new Font("Dialog", Font.PLAIN, SIZE));
fonts.add(new Font("DialogInput", Font.PLAIN, SIZE));
final JList fontList = new JList(fonts.toArray());
fontList.setVisibleRowCount(4);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.setCellRenderer(new FontCellRenderer());
JScrollPane scrollPane = new JScrollPane(fontList); JPanel p = new JPanel();
p.add(scrollPane);
f.add(p);
f.setVisible(true);
fontList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
Font font = (Font) fontList.getSelectedValue();
fontList.setFont(font);
}
});
}
}class FontCellRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Font font = (Font) value;
setText(font.getFamily());
setFont(font);
setOpaque(true);
setBackground(isSelected ? list.getSelectionBackground() : list
.getBackground());
setForeground((index%2==0)?Color.red:Color.blue); //这个是你所要的
return this;
}
}