Java:无法追加textArea

时间:2011-11-04 17:44:08

标签: java methods applet textarea textfield

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

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

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);

}

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

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

1 个答案:

答案 0 :(得分:5)

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

要在AWT中执行此操作,请使用invokeLater()将自定义Runnable注入EventQueue

相关问题