gpt4 book ai didi

java-me - 将图标添加到列表中的每个列表项

转载 作者:行者123 更新时间:2023-12-04 00:47:18 26 4
gpt4 key购买 nike

我想为图像列表中的每个项目添加一个图标。这是我创建列表的代码:

    Form f3=new Form("DEMO FORM");
f3.setScrollable(true);
f3.setLayout(new BorderLayout());
f3.setLayout(new BoxLayout(BoxLayout.Y_AXIS));


String items[] = {"one","two","three","four"};
DefaultListModel myListModel = new DefaultListModel(items);
List lst=new List(myListModel);

f3.addComponent(lst);
f3.show();

我该怎么做?

最佳答案

使用这个列表渲染器

import com.sun.lwuit.Component;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import java.io.IOException;
public class MyListRenderer extends Label implements ListCellRenderer {

private Image[] images;
/** Creates a new instance of MyListRenderer */

public MyListRenderer() {
super("");
images = new Image[2];
try {
images[0] = Image.createImage("/on.png");
images[1] = Image.createImage("/off.png");
} catch (IOException ex) {
ex.printStackTrace();
}
}

public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
setText(value.toString());
//getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,Font.SIZE_MEDIUM));
if (isSelected) {
setFocus(true);
setIcon(images[1]);
getStyle().setBgColor(0xffcc99);
getStyle().setBgTransparency(55);
getStyle().setBorder(Border.createRoundBorder(15, 15, 0xff9900, true));
} else {
setFocus(false);
setIcon(images[0]);
getStyle().setBgColor(0xffffff);
getStyle().setFgColor(0x000000);
getStyle().setBorder(Border.createRoundBorder(15, 15, 0xffcc99, true));
getStyle().setBgTransparency(255);
}
return this;
}

public Component getListFocusComponent(List list) {
setIcon(images[1]);
setText("");
getStyle().setBgColor(0x0000ff);//no effect
setFocus(true);
getStyle().setBgTransparency(100);
return this;
}
}

您可以从此渲染器中删除不需要的修饰:焦点颜色变化等,...我还为未选择和已选择的列表项提供了两个不同图标的代码。然后像这样设置列表的渲染器:

lst.setListCellRenderer(new MyListRenderer());

关于java-me - 将图标添加到列表中的每个列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com