JButton   bt=new   JButton(); 
    bt.addActionListener(new   ActionListener(){ 
public   void   actionPerformed(ActionEvent   e)   { 
Runnable   r=new   Runnable(){ 
public   void   run(){ 
操作序列; 

}; 
SwingUtilities.invokeLater(r); 
} }); 如果我这么调用是异步调用的话,那么点过按妞以后应该马上弹出,可实际上按妞按下去以后一直到操作执行完毕以后才弹出,这是为什么?感觉还是同步

解决方案 »

  1.   

    你在actionLister里面只是定义了一个一个runnable并么有马上运行,所以按按钮以后就没有马上弹出啊!
    如果你需要在按按钮之后马上弹出那么应该是用synchronized,意思就是在actionListener里面直接调用r.run()
      

  2.   

    另外起个线程?
    Runnable   r=new   Runnable(){  
    public   void   run(){  
    操作序列;  

    Thread thread = new Thread(r);
    thread.start();
      

  3.   

    This call will block until all pending AWT events have been processed and (then) doRun.run() returns. This method should be used when an application thread needs to update the GUI. It should not be called from the EventDispatchThread. Here's an example that creates a new application thread that uses invokeAndWait() to print a string from the event dispatching thread and then, when that's finished, print a string from the application thread.final Runnable doHelloWorld = new Runnable() {
         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(). 
      

  4.   

    invokeLaterpublic static void invokeLater(Runnable doRun)    Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.     Runnable doHelloWorld = new Runnable() {
             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)
      

  5.   

    上文有一句will still be deferred until all pending events have been processed!!!!
    所以你直接在按按钮的时候调用r.run()让另外一条线程帮你工作就可以了,然后你在主程序建立一块空间,让这条线程更新这个空间里面的内容,然后加上一个,初始化是false,然后如果线程完成工作,那么是true,如果结果是必须的,或者你可以设置timeout. 在程序末端你获取线程处理数据的时候,你调用一个while+timeout来获取这块空间里面的内容.我大概构思是 JButton b;
    b.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    r.run(); //<- 在Class里面定义(如果你需要处理并且获得数据)
    }
    });