【发布时间】:2013-03-30 20:27:58
【问题描述】:
在我的程序的一部分中,我有一个 JList,它有一个位置列表,我得到一个 API,它应该使用来自 JList 的项目并打印出该位置的天气。所以现在我做不到,因为我用了
WeatherAPI chosen = locList.getSelectedIndex();
但出现错误:类型不匹配:无法从 int 转换为 WeatherAPI。
这是有效的 API 示例:
LinkedList<WeatherAPI> stations = FetchForecast.findStationsNearTo("cityname");
for (WeatherAPI station : stations) {
System.out.println(station);
}
WeatherAPI firstMatch = stations.getFirst();
所以我不想获得第一个选项,我想获得用户选择的位置。这都是关于铸造的。 我也试过这个没有用:
WeatherAPI stations;
WeatherAPI firstMatch = stations.get(locList.getSelectedIndex());
我得到了其余的代码,它使用了“firstMatch,但它只在它的类型是 WeatherAPI 时才使用它。
【问题讨论】:
-
试试 .getSelectedValues()?
-
类型不匹配:无法从 Object 转换为 WeatherAPI
-
交叉邮件:coderanch.com/t/608471/GUI/java/selected-item-JList-casting。为什么要浪费大家的时间发布已经回答的问题?
标签: java swing casting jlist getselection