[英]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.