【发布时间】:2015-04-23 08:02:05
【问题描述】:
经过多次尝试使JProgressBar 按预期工作,我终于成功地实现了我的目标。我曾使用@MadProgrammer 的advice 并使用SwingWorker 最终让程序按我的意愿工作。
现在,我想让光标变成
当我的JProgressBar 从 0% 变为 100% 时。我用谷歌搜索了一下,发现
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
是执行此操作的代码。我已经尝试过了,但它没有按预期工作。
相关代码:
JProgressBar progress;
JButton button;
JDialog dialog; //Fields of my GUI class
progress=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
button=new JButton("Done");
dialog=new JDialog(); //Done from methods
progress.setValue(0);
progress.setStringPainted(true);
progress.setBorderPainted(true); //Also done from methods
button.addActionListener(this); //Also done from methods
dialog.setLayout(new FlowLayout(FlowLayout.CENTER));
dialog.setTitle("Please wait...");
dialog.setBounds(475,150,250,100);
dialog.setModal(true); //Also done from methods
dialog.add(new JLabel("Loading..."));
dialog.add(progress); //Also done from methods
这里是actionPerformed 方法:
public void actionPerformed(ActionEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Task task=new Task();
task.addPropertyChangeListener(this);
task.execute();
dialog.setVisible(true);
}
propertyChange 方法:
public void propertyChange(PropertyChangeEvent evt) {
if("progress" == evt.getPropertyName()){
int progressnum = (Integer) evt.getNewValue();
progress.setValue(progressnum);
}
}
还有嵌套类Task:
class Task extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {
int progressnum = 0;
setProgress(0);
while (progressnum < 100) {
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
System.err.println("An error occured:"+ex);
ex.printStackTrace();
}
progressnum ++;
setProgress(Math.min(progressnum, 100));
}
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
//setCursor(null); //turn off the wait cursor
setCursor(Cursor.getDefaultCursor()); //Is this one or the one above it right?
dialog.dispose();
progress.setValue(progress.getMinimum());
}
}
当我按下button 时,会出现带有 JProgressBar 的 JDialog,并且 JProgressBar 从 0% 变为 100%。在此期间,需要将光标变为(忙碌光标),当JProgressBar达到100%时,正常光标(
) 需要恢复。
问题是,当我按下button 时,光标会在一瞬间变为忙碌光标,然后又变回原来的光标。我希望光标处于忙碌状态,直到 JProgressBar 达到 100%。
我在actionPerformed 方法中添加了将光标转换为忙碌光标的代码,并在嵌套类Task 的done 方法中添加了恢复正常光标的代码。请注意,我还包括了必要的软件包。
- 导致问题的原因是什么?
- 如何解决?
-
我应该使用
setCursor(null);或
setCursor(Cursor.getDefaultCursor());恢复光标?
【问题讨论】:
标签: java swing cursor swingworker jprogressbar