我需要在我的程序中一直运行一个并行函数。 我不知道我的想法是否正确,但我想做的是让一段代码一直更新我的 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/