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
.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){
}
}
});
Hi there, having a bit of a pain with the last stage of this application I've designed.
Basically, when the user clicks the button, I'd like it so the cursor becomes a 'waiting' version and then once the background process (task.execute) has completed, the cursor returns to normal.
The task.execute isn't in the same class so I can't just do a straight call of the "gui.setCursor" because it doesn't recognise GUI as the variable.
Not sure what to do so any advice would be great
Thanks :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修改任务的类,以便它将您的 GUI 作为构造函数参数。这样,当任务完成时,它可以调用
setCursor
方法。您应该使用 SwingWorker 来做这种事情。
编辑:
任务的代码应该是这样的:
Modify the task's class so that it takes your GUI as a constructor argument. This way, when the task is completed, it can invoke the
setCursor
method.You should use a SwingWorker for this kind of thing.
EDIT :
Here's how the code of the task should be :
当您创建任务时,向其传递一个具有某种“已完成”方法的接口。让您的任务在完成时调用该方法,然后让您的 gui 类实现该接口并更改该方法调用上的光标。
When you create your task pass it an interface that has some sort of "completed" method. Make your task call that method when it is finished then have your gui class implement that interface and change the cursor on that method call.
也许你可以尝试将 gui 定为最终版本。
Maybe you can try to make gui final.