【问题标题】:How do I list nicely in a Jlist?如何在 Jlist 中很好地列出?
【发布时间】:2013-03-25 08:58:45
【问题描述】:

我正在制作一个旅行计划桌面应用程序。 我已经到了这么远,所以它只剩下清单了。 我想将 Jlist 与 defaultListModel 或其他东西一起使用。 但是我如何列出所有这样的???

我想得到的结果是这样的:

line : Spårvagn 6
direction : Kortedala via Sahlgrenska
Starting Location : Brunnsparken, Göteborg
time : 17:27
date : 2013-04-03
track : E 
Destination : Svingeln, Göteborg
time : 17:56
date : 2013-04-03
track : B 
line : Spårvagn 1
direction : Östra Sjukhuset
Starting Location : Brunnsparken, Göteborg
time : 17:30
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:36
date : 2013-04-03
track : B 
line : Spårvagn 3
direction : Kålltorp
Starting Location : Brunnsparken, Göteborg
time : 17:35
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:41
date : 2013-04-03
track : B 
line : Spårvagn 6
direction : Kortedala via Sahlgrenska
Starting Location : Brunnsparken, Göteborg
time : 17:36
date : 2013-04-03
track : E 
Destination : Svingeln, Göteborg
time : 18:05
date : 2013-04-03
track : B 
line : Spårvagn 1
direction : Östra Sjukhuset
Starting Location : Brunnsparken, Göteborg
time : 17:40
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:46
date : 2013-04-03
track : B 

我的代码:

JSONObject destination = (JSONObject) jsonObj.get("Destination");
            String dname = (String) destination.get("name");
            String dtime = (String) destination.get("time");
            String ddate = (String) destination.get("date");
            String dtrack =(String) destination.get("track");

            //set the info about the destination
            TripService.getTrip().setDate(date);
            TripService.getTrip().setName(dname);
            TripService.getTrip().setTime(dtime);
            TripService.getTrip().setTrack(dtrack);

            //Sets line and direction for all trips
/*          
            model.addElement("line : " + line);
            model.addElement("direction : " + direction);

            //set the info about the starting location

            model.addElement("Starting Location : " + name);
            model.addElement("Time : " + time);
            model.addElement("Date : " + date);
            model.addElement("Track : " + track);
*/  

但它在我的控制台中返回一个 []。 如何做到这一点

所有这些都仅在我的控制台中与此输出一起列出。但我无法将其转移到我的应用程序中。出了什么问题以及如何做到这一点?

【问题讨论】:

  • 你有没有尝试过?如果是这样,请将其发布,以便我们一起讨论。
  • 是的,我尝试了 LDefaultListModel
  • 当我打印出结果时它会给出一个[]。

标签: java json swing jlist cellrenderer


【解决方案1】:

问题在于没有将数据添加到 ListModel。您所要做的就是创建一个包含您想要显示的所有信息的对象并将该对象添加到模型中。

问题在于显示所有信息。为此,您将需要一个自定义渲染器。请参阅 Writing a Custom Cell Renderer 上的 Swing 教程部分。

【讨论】:

    【解决方案2】:
    DefaultListModel model = new DefaultListModel();
       JList list = new JList(model);
    

    现在使用 model.addElement() 在模型中添加数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 2020-07-23
      • 2015-01-04
      • 2013-10-08
      相关资源
      最近更新 更多