将默认光标更改为忙光标不能按预期工作 [英] Changing the default cursor to busy cursor does not work as expected

查看:164
本文介绍了将默认光标更改为忙光标不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试让 JProgressBar 按预期工作后,我终于成功达到了我的目标。我使用了 @MadProgrammer 建议,并使用 SwingWorker 终于获得程序



现在,我想将光标更改为 BUSY_CURSOR http://telcontar.net/Misc/screeniecursors/Cursor%20hourglass%20white.png 当我的 JProgressBar 从0%到100%。我已经google了,发现

  setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 

是这样做的代码。



相关代码段:

  JProgressBar进度; 
JButton按钮;
JDialog对话框; //我的GUI类的字段

progress = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
button = new JButton(Done);
dialog = new JDialog(); //从方法完成

progress.setValue(0);
progress.setStringPainted(true);
progress.setBorderPainted(true); //也通过方法实现

button.addActionListener(this); //也从方法

dialog.setLayout(new FlowLayout(FlowLayout.CENTER));
dialog.setTitle(Please wait ...);
dialog.setBounds(475,150,250,100);
dialog.setModal(true); //也从方法

dialog.add(new JLabel(Loading ...));
dialog.add(progress); //也从方法

这里是 actionPerformed method:

  public void actionPerformed(ActionEvent e)
{
setCursor(Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));

任务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);
}
}

和嵌套类

  class Task扩展SwingWorker< Void,Void> {
/ *
*主要任务。在后台线程中执行。
* /
@Override
public Void doInBackground(){
int progressnum = 0;
setProgress(0);

while(progressnum <100){
try {
Thread.sleep(10);
} catch(InterruptedException ex){
System.err.println(发生错误:+ ex);
ex.printStackTrace();
}

progressnum ++;
setProgress(Math.min(progressnum,100));
}
return null;
}

/ *
*在事件分派线程中执行
* /
@Override
public void done(){
// setCursor(null); //关闭等待游标
setCursor(Cursor.getDefaultCursor()); //这是一个还是上面的一个是正确的?
dialog.dispose();
progress.setValue(progress.getMinimum());
}
}

当我按按钮,JDialog与JProgressBar出现,JProgressBar从0%到100%。在此期间,光标需要更改为 BUSY_CURSOR http:// telcontar。 net / Misc / screeniecursors / Cursor%20hourglass%20white.png (忙碌光标),当JProgressBar达到100%时,正常光标( NORMAL_CURSOR http://fc05.deviantart.net/fs70/i/2011/008/1/4/mouse_cursor_windows_by_mikima-d36oslj。 png )需要恢复。



问题是,当我按下按钮更改为忙碌光标的分割秒,然后,更改回原始光标。我想让光标忙,直到JProgressBar达到100%。



我已经添加了代码,将游标转换为 actionPerformed 方法并在嵌套类的 done 方法中恢复正常游标 Task 。注意,我还包括必要的包。




  • 导致问题的原因是什么?

  • 如何解决?

  • 我应该使用

      setCursor 

      setCursor(Cursor.getDefaultCursor()); 

    还原光标?


当你在 JFrame 上调用 setCursor() / code>,它只对框架有效,而不是对话框。类似地,当在 JDialog 上调用 setCursor()时,它仅对对话框生效,有问题:你没有设置对话框的光标,对吗?



我不知道 setCursor(null) setCursor(Cursor.getDefaultCursor())更安全。 :p


After many attempts trying to make a JProgressBar work as expected, I finally became successful at achieving my goal. I had used @MadProgrammer's advice and used a SwingWorker to finally get the program work as I want.

Now, I want the cursor to change into BUSY_CURSOR http://telcontar.net/Misc/screeniecursors/Cursor%20hourglass%20white.png when my JProgressBar goes from 0% to 100%. I've googled and found out that

setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

is the code to do it. I've tried it but it does not work as expected.

Relevant piece of code:

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

And here is the actionPerformed method:

public void actionPerformed(ActionEvent e)
{
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    Task task=new Task();
    task.addPropertyChangeListener(this);
    task.execute();
    dialog.setVisible(true);
}

The propertyChange method:

public void propertyChange(PropertyChangeEvent evt) {
    if("progress" == evt.getPropertyName()){
        int progressnum = (Integer) evt.getNewValue();
        progress.setValue(progressnum);
    }
}

and the nested class 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());
        }
    }

When I press button, the JDialog with the JProgressBar appears and the JProgressBar goes from 0% to 100%. During this time, the cursor needs to be changed into BUSY_CURSOR http://telcontar.net/Misc/screeniecursors/Cursor%20hourglass%20white.png(busy cursor) and when the JProgressBar reaches 100%, the normal cursor(NORMAL_CURSOR http://fc05.deviantart.net/fs70/i/2011/008/1/4/mouse_cursor_windows_by_mikima-d36oslj.png) needs to be restored.

The problem is, when I press button, the cursor changes to the busy cursor for a split second and then, changes back into the original cursor. I want the cursor to be busy until the JProgressBar reaches 100%.

I've added the code to convert the cursor into a busy cursor in the actionPerformed method and the restore the normal cursor in the done method of the nested class Task. Note that I've also included the necessary packages.

  • What is causing the problem?
  • How do I fix it?
  • Should I use

    setCursor(null);
    

    or

    setCursor(Cursor.getDefaultCursor());
    

    to restore the cursor?

解决方案

When you call setCursor() on the JFrame, it only has effect for the frame, not the dialog. Similarly, when you call setCursor() on the JDialog, it only has effect for the dialog, not the frame. There the problem: you did not set the cursor for the dialog, right?

I'm not sure whether setCursor(null) is safer than setCursor(Cursor.getDefaultCursor()). :p

这篇关于将默认光标更改为忙光标不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆