JList 不支持直接滚动。要创建滚动列表,需要让 JList 作为 JScrollPane 的视口视图。例如: JScrollPane scrollPane = new JScrollPane(dataList); // Or in two steps: JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().setView(dataList);
错了错了,不好意思啊,需要重写ListCellRender JList 使用 java.awt.Component(由名为 cellRendererer 的委派提供)在列表中绘制可见单元。单元渲染器组件类似于“橡皮图章”,用于绘制每个可见行。每当 JList 需要绘制单元时,它就要求单元渲染器提供组件,使用 setBounds() 将其移动到位,然后通过调用其绘制方法来绘制。默认的单元渲染器使用 JLabel 组件呈现每个组件的字符串值。用户还可以使用如下代码替代自己的单元渲染器: // Display an icon and a string for each object in the list. 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, Object value, // value to display int index, // cell index boolean isSelected, // is the cell selected boolean cellHasFocus) // the list and the cell have the focus { String s = value.toString(); setText(s); setIcon((s.length() > 10) ? longIcon : shortIcon); 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; } } String[] data = {"one", "two", "three", "four"}; JList dataList = new JList(data); dataList.setCellRenderer(new MyCellRenderer());
ScrollPane scrollPane = new JScrollPane(dataList);
// Or in two steps:
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().setView(dataList);
JList 使用 java.awt.Component(由名为 cellRendererer 的委派提供)在列表中绘制可见单元。单元渲染器组件类似于“橡皮图章”,用于绘制每个可见行。每当 JList 需要绘制单元时,它就要求单元渲染器提供组件,使用 setBounds() 将其移动到位,然后通过调用其绘制方法来绘制。默认的单元渲染器使用 JLabel 组件呈现每个组件的字符串值。用户还可以使用如下代码替代自己的单元渲染器: // Display an icon and a string for each object in the list.
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,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
String s = value.toString();
setText(s);
setIcon((s.length() > 10) ? longIcon : shortIcon);
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;
}
} String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setCellRenderer(new MyCellRenderer());