Java:无法追加textArea

Bla*_*ein 1 java methods applet textarea textfield

public void actionPerformed(ActionEvent evt) {
    input = textField.getText();
    textArea.append(input);
    textField.setText("");
    textArea.setCaretPosition(textArea.getDocument().getLength());
}
Run Code Online (Sandbox Code Playgroud)

此方法有效,并且只要调用方法,就会附加变量输入.

public void start(){

    System.out.println("Starting");
    int questionNumber = 0;
    Counter counter = new Counter();
    counter = pickQuestions();
    System.out.println("here");
    textArea.append("**Applet**");
    System.out.println("now here");
    doQuestion(counter, questionNumber);

}
Run Code Online (Sandbox Code Playgroud)

此方法不起作用,并且不附加" Applet ",但它会打印"here",然后"now here".

有没有人知道可能的原因或者我没有提供足够的信息?谢谢!

Law*_*Dol 5

问题是在第二种情况下,您正在从GUI事件调度线程以外的线程更新GUI组件.必须从EDT完成对GUI组件的任何操作.

要在AWT为此,注入一个定制的可运行EventQueue中使用invokeLater().