栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何生成具有交替颜色的Jlist

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何生成具有交替颜色的Jlist

要自定义

JList
单元格的外观,您需要编写自己的实现
ListCellRenderer

的示例实现

class
可能如下所示:(草绘,未经测试)

public class MyListCellThing extends JLabel implements ListCellRenderer {    public MyListCellThing() {        setOpaque(true);    }    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {        // Assumes the stuff in the list has a pretty toString        setText(value.toString());        // based on the index you set the color.  This produces the every other effect.        if (index % 2 == 0) setBackground(Color.RED);        else setBackground(Color.BLUE);        return this;    }}

要使用此渲染器,请在您

JList
的构造函数中添加以下代码:

setCellRenderer(new MyListCellThing());

要基于所选对象并具有焦点来更改单元格的行为,请使用提供的布尔值。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454417.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号