Java:无法追加文本区域

发布于 2024-12-13 18:14:09 字数 695 浏览 3 评论 0原文

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”,但它会打印“此处”,然后打印“现在此处”。

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

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

This method works and will append the variable input whenever the method is called.

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

}

This method does not work, and does not append "Applet", however it does print "here", and then "now here".

Does anybody know a possible reason for this or have I not given enough information?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

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

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

The problem is that in the second case you are updating a GUI component from a thread other than the GUI Event Dispatch Thread. Any manipulation of a GUI component must be done from the EDT.

To do this in AWT, inject a custom Runnable into the EventQueue using invokeLater().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文