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

时间:2021-05-01 15:40:27

I have ArrayList where I put Author, Title and Value(integer). I want to put them in label and print in separate lines. But what I get is that text is printed in one line.

我有ArrayList,我把Author,Title和Value(整数)放在一起。我想把它们放在标签上,然后分开打印。但我得到的是文本打印在一行。

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

Why \n does not work?

为什么\ n不起作用?

EDIT: Solution:

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 个解决方案

#1


1  

You can use HTML tags in your JLabel. Try to seperate your Strings with that break tag: <br>

您可以在JLabel中使用HTML标记。尝试用断点标记分隔你的字符串:

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

NOTE: Your JLabel text has to start with the opening <html> and end with the closing </html>.

注意:您的JLabel文本必须以开头开头,以结束 结束。

#1


1  

You can use HTML tags in your JLabel. Try to seperate your Strings with that break tag: <br>

您可以在JLabel中使用HTML标记。尝试用断点标记分隔你的字符串:

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

NOTE: Your JLabel text has to start with the opening <html> and end with the closing </html>.

注意:您的JLabel文本必须以开头开头,以结束 结束。

相关文章