dn96 2016-12-22 14:22 采纳率: 50%
浏览 1459

JList中的Renderer获取问题,当根据索引index值设置显示的文本内容时,文本显示全都一样

我重写ListCellRenderer类,继承JPanel。在JPanel中添加一个JLabel用于显示list中的索引。但是设置setText(String.valueOf(index))之后显示的值都为0.如果用控制台直接输入index又是对的,请问有没有大神知道为什么,感觉涉及了内存的问题?

代码如下:

 @Override
    public Component getListCellRendererComponent(JList<? extends Vehicle> list, Vehicle value, int index,
            boolean isSelected, boolean cellHasFocus) {

        // 绘制图标
        JLabel iconLabel = new JLabel();
        // 绘制车位号
        JLabel numLabel = new JLabel();
        // 绘制车牌号,没有显示位空车位
        JLabel vehicleNumber = new JLabel();

        if (value.isIsInParking()) {
            iconLabel.setIcon(iconNotNull);
        } else {
            iconLabel.setIcon(iconNull);
        }
        JPanel temp1 = new JPanel(new FlowLayout());
        temp1.setPreferredSize(new Dimension(50, 50));
        temp1.add(iconLabel);
        temp1.setBackground(CellColr);

        numLabel.setText(String.valueOf(index));    //显示出来为0
        System.out.println(num);    //正常显示0 1 2 3 4 ...
        // numLabel.setText(String.valueOf(num) + "号车位");
        vehicleNumber.setText(value.getLicensePlateNumber());
        numLabel.setForeground(Color.orange);
        vehicleNumber.setForeground(Color.orange);
        numLabel.setFont(font);
        vehicleNumber.setFont(font);

        JPanel temp2 = new JPanel(new FlowLayout());
        Box box = Box.createVerticalBox();
        box.add(numLabel);
        box.add(Box.createVerticalStrut(8));
        box.add(vehicleNumber);

        temp2.setBackground(CellColr);
        temp2.add(box);

        add(temp1, BorderLayout.WEST);
        add(temp2, BorderLayout.CENTER);
        return this;
    }

图:
图片说明

谢谢了~

  • 写回答

2条回答 默认 最新

  • devmiao 2016-12-22 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?