1

我有一个包含一些项目的 JList,然后我有一个 API 需要获取所选项目并使用它来提供有关该项目的一些信息。我的问题是 JList 的类型是 String 而我的 API 的类型显然是不同的。让我们说LocationAPI。所以 API 只从它自己的类型而不是其他类型获取索引。我正在寻找一种将所选项目(字符串)转换为适合 API 的解决方案。这是 API 的一个简单示例。

LinkedList<LocationAPI> stations = FetchWeatherForecast.findWeatherStationsNearTo("cityName");
for (LocationAPI station : stations) { System.out.println(station); }

// Assume the first match on the list is correct
LocationAPI firstMatch = stations.getFirst();

List<ForecastForOneDay> forecast = FetchWeatherForecast.getWeatherForecast(firstMatch);

所以这是 API 工作原理的一个简单示例,但我必须使用我自己的项目和列表,而不是“firstMatch”和“Station”。我得到了项目和列表,但我以不同的方式制作它们:

DefaultListModel<String> cityPlace = new DefaultListModel<String>();
JList myList = new JList(cityPlace);

那么如何在 API 中使用上述列表中的项目呢?

4

0 回答 0