JAVA父类多态指向子类对象,然后用一个子类对象来恢复原子类对象,为什么会失败?

love277369986 2011-11-16 05:14:34
/* 可以显示图标的ListCell绘制器 */
public class IconListItemRenderer extends JLabel implements ListCellRenderer
{
private Border
selectedBorder = BorderFactory.createLineBorder(Color.blue,1),
emptyBorder = BorderFactory.createEmptyBorder(1,1,1,1);
IconListItem item ;

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
try
{
if(value instanceof IconListItem){
item = (IconListItem)value;
}
}catch(Exception e){System.out.println("转换失败!");}

this.setIcon(item.getIcon());
this.setText(item.getText());



if ( isSelected ) setBorder (selectedBorder);
else setBorder(emptyBorder);
return this;
}
}

this.setIcon(item.getIcon());this.setText(item.getText());导致运行时失败,知道错什么的告诉下,谢谢!
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧