繁体   English   中英

如何在jlabel上显示字符串的数组列表?

[英]How to show a arraylist of string on jlabel?

嗨,我有一个字符串数组列表,我想在JLabel上显示该数组列表的内容,用空格或逗号分隔。 但是它只显示了一个String,最后一个是String。

 public void ShowMovie(int idMovie) throws SQLException, IOException {
        int ID = idMovie;
        String IDMOVIE = Integer.toString(ID);
        IDMovieLabel.setText(IDMOVIE);

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Cover.class.getName()).log(Level.SEVERE, null, ex);
        }
        con = DriverManager.getConnection("jdbc:mysql://localhost/whichmovie", "Asis", "dekrayat24");

        String sql = "SELECT Title,Year,Country,recomendacion,Cover,Rating,NameDirec,Name FROM movie "
                + "Inner join direction on (movie.idMovie=direction.idMovie5)"
                + "Inner join director on (direction.idDirector=director.idDirector)"
                + "Inner join cast on (movie.idMovie=cast.idMovie4)"
                + "Inner join actor on (cast.idActor=actor.idActor)"
                + "where idMovie= '" + ID + "'";

        st = con.prepareStatement(sql);
        rs = st.executeQuery(sql);

        while (rs.next()) {

            String titulo = rs.getString(1);
            int añoInt = rs.getInt(2);
            String año = Integer.toString(añoInt);

            byte[] imagedataCover = rs.getBytes("Country");
            byte[] imagedataCover1 = rs.getBytes("Cover");
            format = new ImageIcon(imagedataCover);
            format2 = new ImageIcon(imagedataCover1);

            TituloLabel.setText(titulo);
            AñoLabel.setText(año);
            CountryLabel.setIcon(format);
            DirectorLabel.setText(rs.getString(7));

            int Recomend = rs.getInt(4);
            String Recom = Integer.toString(Recomend);

            RecommendLabel.setText(Recom);

            int Rating = rs.getInt(6);
            String Rat = Integer.toString(Rating);

            RatingLabel.setText(Rat);
            starRater1.setSelection(Rating);
            starRater1.setEnabled(false);

            Image imgEscalada = format2.getImage().getScaledInstance(CoverLabel.getWidth(),
                    CoverLabel.getHeight(), Image.SCALE_SMOOTH);
            Icon iconoEscalado = new ImageIcon(imgEscalada);
            CoverLabel.setIcon(iconoEscalado);

            ArrayList<String> actors = new ArrayList<>();
            actors.add(rs.getString(8));
            System.out.println(actors);// Here i can see i get 9 actors.

            StringBuilder sb = new StringBuilder();

            boolean first = true;
            for (String s : actors) {
                if (!first) {
                    sb.append(' ');
                }
                sb.append(s);
                first = false;
            }

            CastLabel1.setText(sb.toString());

        }
        rs.close();
        st.close();
        con.close();

    }

有什么帮助吗?

编辑:不幸的是,没有解决方案对我有帮助,也许我在方法上做错了,我发布了完整的方法。

问题是您要重置for循环中每个步骤的标签,而不是创建累积结果。 见下文:

StringBuilder buf = new StringBuilder();
for(int i = 0; i < actors.size(); i++){
    buf.append(actors.get(i));
    if(i < actors.size() -1){
        buf.append(" ");
    }
}
CastLabel1.setText(buf.toString())
String text = ""; 
for(int i = 0; i < actors.size(); i++){
   text = text + actors.get(i);
   if(i < actors.size() - 2){
        text = text + ", ";   
   }
} 
CastLabel1.setText(text);

你应该建立你想显示第一 ,然后将其设置为标签的文本字符串:

StringBuilder sb = new StringBuilder();

boolean first = true;
for (String s : actors) {
    if (!first)
        sb.append(' ');
    sb.append(s);
    first = false;
}

CastLabel1.setText(sb.toString());

您当前正在做的是在每次迭代过程中更改整个标签文本,因此最终文本是列表中最后一个元素的文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM