如果你想做我认为你想做的事情,你可以使用 SwingWorker。它有发布方法和处理方法。publish 方法会将消息发送到 process 方法。与 SwingWorker 的其余部分不同,process 方法在 EDT 上运行。
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
public class MyFrame extends JFrame {
private static JTextField txtFld;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
new UpdateSwingWorker().execute();
}
});
}
private static void createAndShowGUI() {
MyFrame frame = new MyFrame();
JPanel panel = new JPanel(new BorderLayout());
txtFld = new JTextField();
panel.add(txtFld, BorderLayout.CENTER);
frame.setSize(new Dimension(300,200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300,200));
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private static class UpdateSwingWorker extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() throws Exception {
// Do your work...
System.out.println("Thread in doInBackground: " + Thread.currentThread());
publish("Updated");
return null;
}
@Override
protected void process(List<String> chunks) {
System.out.println("Thread in process: " + Thread.currentThread());
txtFld.setText(chunks != null ? chunks.get(0) : "");
}
}
}
或者,如果您真的想使用观察者模式,您可以将 notifyObservers 调用包装在 EventQueue 的 invokeLater 调用中。此方法导致 Runnable 在 EDT 中发生其运行方法。
框架:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyFrame2 extends JFrame implements Observer {
private static JTextField txtFld;
private static MyTask task = new MyTask();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
new Thread(task).start();
}
});
}
private static void createAndShowGUI() {
MyFrame2 frame = new MyFrame2();
task.addObserver(frame);
JPanel panel = new JPanel(new BorderLayout());
txtFld = new JTextField();
panel.add(txtFld, BorderLayout.CENTER);
frame.setSize(new Dimension(300,200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300,200));
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
@Override
public void update(Observable o, Object arg) {
System.out.println("Thread in update: " + Thread.currentThread());
txtFld.setText((String) arg);
}
}
任务:
import java.awt.EventQueue;
import java.util.Observable;
public class MyTask extends Observable implements Runnable {
@Override
public void run() {
// Do work
System.out.println("Thread in run: " + Thread.currentThread());
setChanged();
// Notify the observers on the EDT.
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println("Thread in invokeLater: " + Thread.currentThread());
notifyObservers("Update!");
}
});
}
}