java Jlist 的字体怎么没法设置? 代码如下,怎么没效果?
classMyCellRendererextendsJLabelimplementsListCellRenderer{//finalstaticImageIconlong...
class MyCellRenderer extends JLabel implements ListCellRenderer {
// final static ImageIcon longIcon = new ImageIcon("long.gif");
// final static ImageIcon shortIcon = new ImageIcon("short.gif");
// This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called.
public Component getListCellRendererComponent(
JList list, // the list
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // does the cell have focus
{
String s = value.toString();
setText(s);
//this.setFont(new Font("华文行楷",1,12));
setFont(new Font("华文行楷",1,12));
// setIcon((s.length() > 10) ? longIcon : shortIcon);
if(cellHasFocus){
setForeground(Color.red);
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
} 展开
// final static ImageIcon longIcon = new ImageIcon("long.gif");
// final static ImageIcon shortIcon = new ImageIcon("short.gif");
// This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called.
public Component getListCellRendererComponent(
JList list, // the list
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // does the cell have focus
{
String s = value.toString();
setText(s);
//this.setFont(new Font("华文行楷",1,12));
setFont(new Font("华文行楷",1,12));
// setIcon((s.length() > 10) ? longIcon : shortIcon);
if(cellHasFocus){
setForeground(Color.red);
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询