所以我正在努力制作一个类,将字母“键入”到 JTextArea 中,在每个字母之间暂停以使其看起来像是有人在键入。我能想到的唯一方法是在一个暂停的循环中使用 output.append(char) 。唯一的问题...您只能将字符串 append 到 JTextAreas。
那么有什么方法可以将 char 转换为字符串,以便我可以逐个字母地 append 它???
到目前为止,这是我的代码:
import objectdraw.*;
import java.awt.*;
import javax.swing.*;
public class TypeWriter extends ActiveObject
implements Drawable {
private char [] letter;
private JTextArea cp;
public TypeWriter(String sentence, JTextArea console) {
/* break string into characters and save the console
* for later use
*/
letter = new char[sentence.length()];
cp = console;
for(int i=0; i<sentence.length(); i++) {
letter[i] = sentence.charAt(i);
}
start();
}
public void run() {
// append each letter, pausing between them
for( char s : letter ) {
cp.append(new String(s));
pause(50);
}
}
/* More methods for the
* Drawable interface...
*/
}
最佳答案
所以使用 String(char[] value) 构造函数
String s = "";
s += Character.toString('s');
关于java - 将一个字符追加到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8070681/