gpt4 book ai didi

java - 将数组列表中的文本放在单独的行中

转载 作者:行者123 更新时间:2023-12-01 13:15:51 24 4
gpt4 key购买 nike

我有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>");

最佳答案

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

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

注意:您的 JLabel 文本必须以 <html> 开头并以结束语 </html> 结束.

关于java - 将数组列表中的文本放在单独的行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com