[英]How to add text to new line
我想将文本添加到新行。
我的代码:
TextField text = new TextField();
TextArea area = new Area();
String txt = text.getValue().toString();
area.setValue("\n" + txt);
当我单击按钮时,我从TextField中看到了我的值。 我想在TextArea的新行中添加新文本。 请帮忙。
您必须将该新值附加到现有值上并进行设置。 类似于以下内容:
area.setValue(area.getValue() + "\n" + txt);
Vaadin TextArea无法直接添加。 还有java中的规则,何时对字符串使用+
。 如果这样做,请考虑使用StringBuffer
。
代替
area.setValue("\n" + txt);
采用
area.setValue(area.getValue + "\n" + txt);
这将添加文本,而不是替换
我建议在最后添加新行。 您将避免在TextArea的开头使用空行。
例如outputTextArea.setValue(outputTextArea.getValue() + input.getValue() + "\\n");
此外:请务必使用一个文本区域 ,而不是一个文本字段 ,因为符合打破“\\ n”不会在文本字段工作!
请记住,Vaadin可与浏览器一起使用,因此普通的scape字符不起作用,即:您不能在普通Label
上使用'\\ n'。 代替它,执行以下操作:
label.setCaptionAsHtml(true);
label.setValue(label.getValue()+"<br>");
如果使用TextArea
,则可以照常使用'\\n'
。 如果使用TextFiel
,则不能编写多行,因为它将在TextArea
对其进行转换,因此不建议使用此行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.