简体   繁体   English

如何在Label JavaFX中添加多行

[英]How to add multiple lines in Label JavaFX

I have a problem that I don't really know how to add a multiple lines into Label in JavaFX. 我有一个问题,我真的不知道如何在JavaFX中向Label中添加多行。

For example: 例如:

 Label label = new Label();
 for(int i= 0; i<10; i++){
     label.setText(Integer.toString(i));
 }

So when the loop finishes, the label just only shows the final value which is 9. 因此,当循环结束时,标签只显示最终值9。

So any solutions that can show all the numbers 1 - 9 with the break lines( such as '\\n') between them. 因此任何解决方案都可以显示所有数字1 - 9与它们之间的断行(例如'\\ n')。

This problem that happens when i want to show the Bill of my project that contain many dishes. 当我想要显示包含许多菜肴的项目的Bill时,会发生此问题。 Thank you for your help. 谢谢您的帮助。

You need to append and not set the text over and over again AND you need the new line character '\\n' 您需要添加,而不是重新设置文本一遍又一遍你需要换行字符“\\ n”

my suggestion would be like using a variable to append the information and when you are done with that step, then set the label.text 我的建议就像使用变量来附加信息,当你完成该步骤时,然后设置label.text

Example: 例:

StringBuilder msg = new StringBuilder():
Label label = new Label();
for (int i = 0; i < 10; i++) {
    msg.append(Integer.toString(i));
    msg.append(",\n");  //this is the new line you need
}
label.setText(msg.toString());

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

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