JButton bt=new JButton();
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Runnable r=new Runnable(){
public void run(){
操作序列;
}
};
SwingUtilities.invokeLater(r);
} }); 如果我这么调用是异步调用的话,那么点过按妞以后应该马上弹出,可实际上按妞按下去以后一直到操作执行完毕以后才弹出,这是为什么?感觉还是同步
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Runnable r=new Runnable(){
public void run(){
操作序列;
}
};
SwingUtilities.invokeLater(r);
} }); 如果我这么调用是异步调用的话,那么点过按妞以后应该马上弹出,可实际上按妞按下去以后一直到操作执行完毕以后才弹出,这是为什么?感觉还是同步
如果你需要在按按钮之后马上弹出那么应该是用synchronized,意思就是在actionListener里面直接调用r.run()
Runnable r=new Runnable(){
public void run(){
操作序列;
}
Thread thread = new Thread(r);
thread.start();
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
}; Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doHelloWorld);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished on " + Thread.currentThread());
}
};
appThread.start();Note that if the Runnable.run() method throws an uncaught exception (on the event dispatching thread) it's caught and rethrown, as an InvocationTargetException, on the callers thread.Additional documentation and examples for this method can be found in How to Use Threads, in The Java Tutorial.As of 1.3 this method is just a cover for java.awt.EventQueue.invokeAndWait().
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
}; SwingUtilities.invokeLater(doHelloWorld);
System.out.println("This might well be displayed before the other message.");
If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread). Additional documentation and examples for this method can be found in How to Use Threads, in The Java Tutorial. As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater(). See Also:
invokeAndWait(java.lang.Runnable)
所以你直接在按按钮的时候调用r.run()让另外一条线程帮你工作就可以了,然后你在主程序建立一块空间,让这条线程更新这个空间里面的内容,然后加上一个,初始化是false,然后如果线程完成工作,那么是true,如果结果是必须的,或者你可以设置timeout. 在程序末端你获取线程处理数据的时候,你调用一个while+timeout来获取这块空间里面的内容.我大概构思是 JButton b;
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
r.run(); //<- 在Class里面定义(如果你需要处理并且获得数据)
}
});