我需要在我的程序中一直运行并行函数。 我不知道我的想法是否正确,但我想做的是让一段代码一直更新我的TextArea,从服务器获取信息。
我正在使用RMI,只是你知道。
我可以在MainClass中创建一个线程函数,让它一直运行吗?
或者我如何创建一个线程来更新另一个类中的TextArea?我如何分享我的TextArea?
答案 0 :(得分:3)
不确定我是否理解,我会假设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();
}