gpt4 book ai didi

java - 我可以在 Java 中创建一个没有函数的线程吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:08 25 4
gpt4 key购买 nike

我需要在我的程序中一直运行一个并行函数。我不知道我的想法是否正确,但我想做的是让一段代码一直更新我的 TextArea,从服务器获取信息。

我正在使用 RMI,只是你知道。

我可以在 MainClass 中创建一个线程函数,让它一直运行吗?

或者我如何创建一个线程来更新另一个类中的 TextArea?如何共享我的 TextArea?

最佳答案

不确定我是否理解,我假设 TextArea 表示 JTextArea,而 MainClass 是应用程序的入口点。

是什么阻止你这样做?

public class Updater implements Runnable {
private JTextArea textArea;

public Updater(JTextArea textArea){
this.textArea = textArea;
}

@Override
public void run(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
//do what you've got to do....
textArea.setText("New Text");
}
});
}

}

在你的“MainClass”中是这样的:

public static void main(String[] args) {

Thread myThread = new Thread(new Updater(myTextArea));
myThread.start();

}

关于java - 我可以在 Java 中创建一个没有函数的线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com