<< 返回
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
/**
*
*/
private static final long serialVersionUID = 2804034559328831035L;
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// TODO Auto-generated method stub
String s = value.toString();
setText(s);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setOpaque(true); //允许重新绘制所有像素
if ("常规".equals(s) || "倾斜".equals(s) || "粗体".equals(s) || "粗体 倾斜".equals(s)) {
setFont(new Font("宋体", index, 14));
return this;
} else {
setFont(new Font(s, 0, 14));
return this;
}
}
}
<< 返回