请java高手帮看看我的这个程序

来源:百度知道 编辑:UC知道 时间:2024/05/24 14:13:47
我是新手,想编一个使列表同时显示图标和文本的程序,但是编好后用eclipse运行总是出错,不知道哪里出错了,这几天我都想哭了,求各位大虾帮帮忙,在此谢过!
import java.awt.*;
import javax.swing.*;
public class OwnCellRenderer extends JLabel implements ListCellRenderer
//自定义的一个类
{
Icon[] icons;
public OwnCellRenderer(Icon[] icons)
{
this.icons=icons;
this.setOpaque(true);
this.setFont(new Font("Dialog",Font.PLAIN,14));
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if(value!=null)
{
String text=value.toString();
this.setText(text);
}
this.setIcon(icons[index]);

if(isSelected)
{
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelecti

把Icon[] icon={icon[0],icon[1],icon[2],icon[3],icon[4],icon[5]};
改为Icon[] icon = new Icon[6];
看看,其他且不作修改。(突然间想起有点搞笑的事)

就改JListDemo类
加1。Icon[] icon = new Icon[6];
2。icon[0]-icon[5]放在前面
3。Icon[] icons
4。list.setCellRenderer(new OwnCellRenderer(icons));

class JListDemo extends JFrame {
1。Icon[] icon = new Icon[6];
//程序窗口
public JListDemo(){

2。icon[0]=new ImageIcon(ClassLoader.getSystemResource("6.gif"));
icon[1]=new ImageIcon(ClassLoader.getSystemResource("1.gif"));
icon[2]=new ImageIcon(ClassLoader.getSystemResource("2.gif"));
icon[3]=new ImageIcon(ClassLoader.getSystemResource("3.gif"));
icon[4]=new ImageIcon(ClassLoader.getSystemResource("4.gif"));
icon[5]=new ImageIcon(ClassLoader.getSystemResource("5.gif"));
3。Icon[] icons={icon[0],icon[1],icon[2],icon[3],icon[4],icon[5]};
String[] iconnames={"电脑