gpt4 book ai didi

java - LWUIT 组合框 : Class as the Items

转载 作者:行者123 更新时间:2023-11-29 06:01:55 26 4
gpt4 key购买 nike

我在 LWUIT 中使用 ComboBox。我的代码是这样的。

public Locations(String name, int X, int Y)
{
Name = name;
xLocation = X;
yLocation = Y;
}

我已经列出了位置

private List getLocations()
{
List list = new List();
list.addItem(new Locations("Landmark1", 23, 40));
list.addItem(new Locations("Landmark3", 24, 40));
list.addItem(new Locations("Landmark4", 25, 40));
list.addItem(new Locations("Landmark6", 26, 40));

return list;
}

然后使组合框包含列表。

comboBox_Locations = new ComboBox(getLocations().getModel());

现在,我的问题是如何才能在 ComboBox 上只显示位置的名称?我知道我可以像这样列出位置名称的字符串:

private List getLocations()
{
List list = new List();
list.addItem(new Locations("Landmark1", 23, 40).Name);
list.addItem(new Locations("Landmark3", 24, 40).Name);
list.addItem(new Locations("Landmark4", 25, 40).Name);
list.addItem(new Locations("Landmark6", 26, 40).Name);

return list;
}

但是当我获取selectedItem 时,它只能获取名称而无法获取坐标。我想做的是像这样上课:

Object item =  comboBox_Locations.getSelectedItem();
if (item.getClass() == Locations.class)
{
String Name = ((Locations)item).Name.toString();
int xCoords = ((Locations)item).getX();
int yCoords = ((Locations)item).getY();
}

这样我就可以使用 xCoords 和 yCoords。

最佳答案

使用渲染器:

list.setRenderer(new DefaultListCellRenderer() {
public Component getCellRendererComponent(Component list, Object model, Object value, int index, boolean isSelected) {
String t = ((Locations)value).getName();
return super.getCellRendererComponent(list, model, t, index, isSelected);
}
});

关于java - LWUIT 组合框 : Class as the Items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806186/

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