将数组列表中的文本放到单独的行中

时间:2014-03-18 13:35:23

标签: java html string swing label

我有ArrayList,其中我放置了作者,标题和值(整数)。我想把它们放在标签上并分开打印。但我得到的是文字印在一行。

for (Book book : listofbooks) {
    labelis.setText(labelis.getText() + "\nName: " + book.getName() + "Tile: " + book.getTitle() + "Number of books: " + book.getHowMany());
}
panel.add(labelis);

为什么\n不起作用?

编辑:解决方案:

for (Book book : listofbooks) {

        label.setText( "<html> "
        + label.getText() 
        + "<br>Name: " + book.getName()
        + "Tile: " + book.getTitle()
        + "Number of books: " 
        + book.getHowMany()
        );

    }
    label.setText(label.getText()+ "</html>");

1 个答案:

答案 0 :(得分:1)

您可以在JLabel中使用HTML标记。尝试使用该中断标记分隔您的字符串:<br>

    label.setText("<html> +"
            + label.getText() 
            + "<br>Name: " + book.getName()
            + "Tile: " + book.getTitle()
            + "Number of books: " 
            + book.getHowMany()
            + "</html>");

注意:您的JLabel文字必须以开头<html>开头,并以结束</html>结束。