用ListCellRendererclass 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(isSelected
? list.getSelectionForeground()
: list.getForeground());
return this;
}
}调用
ArrayList fonts = new ArrayList();
final int SIZE = 24;
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));
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);
fontList.addListSelectionListener(new
ListSelectionListener()
{
public void valueChanged(ListSelectionEvent evt)
{
Font font = (Font)fontList.getSelectedValue();
text.setFont(font);
} });
{
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(isSelected
? list.getSelectionForeground()
: list.getForeground());
return this;
}
}调用
ArrayList fonts = new ArrayList();
final int SIZE = 24;
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));
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);
fontList.addListSelectionListener(new
ListSelectionListener()
{
public void valueChanged(ListSelectionEvent evt)
{
Font font = (Font)fontList.getSelectedValue();
text.setFont(font);
} });
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货