Java SwingUtilities.invokeLater

.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e){
            try{
                ta.append("Searching Initiated at: "+datetime()+"n");
                gui.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                task.execute();
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }
                });
                //Enable the next stage in the YD process and disable the previously executed functions
                clusAn.setEnabled(true);
                open.setEnabled(false);
                statCl.setEnabled(false);
            }catch (Exception IOE){
                }
        }
    });
嗨,我设计的这个应用程序的最后阶段有点痛苦。 基本上,当用户点击按钮时,我会喜欢它,所以光标变成'等待'版本,然后一旦后台进程(task.execute)完成,光标就会恢复正常。 task.execute不在同一个类中,因此我不能直接调用“gui.setCursor”,因为它不能将GUI识别为变量。 不知道该怎么做,所以任何建议都会很棒 感谢:D     
已邀请:
修改任务的类,使其将GUI作为构造函数参数。这样,当任务完成时,它可以调用
setCursor
方法。 你应该使用SwingWorker来做这种事情。 编辑: 以下是任务代码的编写方式:
public class MySwingWorker extends SwingWorker<Void, Void> {

    /**
     * The frame which must have the default cursor set 
     * at the end of the background task
     */
    private JFrame gui;

    public MySwingWorker(JFrame gui) {
        this.gui = gui;
    }

    // ...

    @Override
    protected void done() {
        // the done method is called in the EDT. 
        // No need for SwingUtilities.invokeLater here
        gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
}
    
在创建任务时,将其传递给具有某种“已完成”方法的接口。让你的任务在完成后调用该方法然后让你的gui类实现该接口并在该方法调用上更改游标。     
也许你可以尝试让gui最终。
final JComponent guiFinal = gui;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        guiFinal .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }
                });
    

要回复问题请先登录注册