简体   繁体   English

JTextArea显示文本和字符串

[英]JTextArea display text and strings

I have a JTextArea and would like to be able to add multiple arguments to like so: messageArea.setText("Hi", name, "how are you today?"); 我有一个JTextArea,希望能够添加多个参数,例如: messageArea.setText("Hi", name, "how are you today?"); However, I am unable to do so, I do not know how to add multiple arguments to the JTextArea. 但是,我无法这样做,我不知道如何向JTextArea添加多个参数。

The setText() method takes a single String argument, so you have to concatenate the strings, you want to display. setText()方法采用单个String参数,因此必须连接要显示的字符串。

StringBuilder sb = new StringBuilder();
sb.append("Hi").append(name).append(", how are you?");
messageArea.setText(sb.toString());

Other method is to simply use the + operator: 另一种方法是简单地使用+运算符:

messageArea.setText("Hi"+name+"...");

Or use the MessageFormat class: 或使用MessageFormat类:

messageArea.setText(MessageFormat.format("Hi {0} how are you?", name));

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

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