.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
发布于 2011-02-23 22:01:58
修改任务的类,使其将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));
}
}发布于 2011-02-23 22:00:21
当你创建你的任务时,给它传递一个具有某种"completed“方法的接口。让您的任务在完成时调用该方法,然后让您的gui类实现该接口并更改该方法调用上的光标。
发布于 2011-03-05 12:35:29
也许你可以试着让gui变成最后的。
final JComponent guiFinal = gui;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
guiFinal .setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
});https://stackoverflow.com/questions/5091835
复制相似问题