有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇人的勇气。invokeLater、SwingWorker和gif图标动画问题

我试图加载一个gif图标,而我在后台做一些昂贵的工作。但问题是,gif(动画)图标未加载

如果我用png替换gif图标,效果很好

下面是测试类代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);

    JButton loginButton = new JButton("Login");
    panel.add(loginButton);
    frame.add(panel);
    frame.setVisible(true);

    loginButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            URL urlProcessing = Test.class.getResource("processing.gif");
            //URL urlProcessing = Test.class.getResource("processing.png");
            ImageIcon processingIcon = new ImageIcon(urlProcessing);
            loginButton.setText("Authenticating...");
            loginButton.setIcon(processingIcon);
            loginButton.revalidate();
            loginButton.repaint();


            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {     

                    Worker worker = new Worker(frame);
                    try {
                        worker.doInBackground();                            
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            });

        }
    });

}
}

当用户点击登录按钮时,我更改了按钮的文本,并在其上放置了gif图标。在

SwingUtilities.invokeLater(new Runnable() {

方法通过SwingWorker线程启动后台昂贵任务。现在,不需要发布和处理SwingWorker类的方法,因为我在后台任务期间不做任何工作

如果我删除“SwingUtilities.invokeLater(new Runnable(){”方法并直接调用SwingWorker类,那么gif和png图标都不起作用(显示在按钮上)

我尝试用简单的线程来改变文本,并在按钮上设置图标,但效果并不理想

下面是SwingWorker类代码

import javax.swing.JFrame;
import javax.swing.SwingWorker;

public class Worker extends SwingWorker<String, String> implements Runnable {

private JFrame testFrame;
// some public and private properties

public Worker(JFrame frame){
    // doing some assignments

    this.testFrame = frame;
}

@Override
protected void done() {
    // TODO Auto-generated method stub
    super.done();
    System.out.println("Done method called");
}


@Override
protected String doInBackground() throws Exception {
    int i = 0;
    String chunks = null;
    publish(chunks);
    // Doing some very expensive work, HTTP request, DB operations etc...
    while (i < 10) {
        Thread.sleep(1000);
        System.out.println("Value of i: "+i);
        i++;
    }
    this.done();
    testFrame.setVisible(false);
    return null;
}
}

知道吗?GUI主线程中发生了什么?错在哪里


共 (1) 个答案

  1. # 1 楼答案

    你不应该直接调用doInBackground,你应该调用execute。您还应该能够在UI线程中进行该调用(即,不需要在invokeLater中调用它)。在doInBackground实现中,应该在UI线程中对UI进行更改(即invokeLater应该在那里使用)