SwingWorker 的问题

发布于 2024-10-20 01:00:57 字数 1545 浏览 4 评论 0原文

我正在尝试让 SwingWorker 工作。 我现在有以下代码:

public class ImageWorker extends SwingWorker<Void, Void> implements KeyListener
{   
private JLabel imageLabel;
private ImageIcon basicImage; 
private ImageIcon whiteImage;  
public static void main(String[] args)
{
    new ImageWorker();      
}

public ImageWorker()
{
    final JFrame frame = new JFrame();
    imageLabel = new JLabel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.getContentPane().add(imageLabel);
    frame.setVisible(true);
    try
    {
        basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
        whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
    }
    catch(IOException ex)
    {
        ex.getMessage();
    }
    this.execute();

}

@Override
protected Void doInBackground() 
{
    try
    {
        while (true)
        {
            displayImage(basicImage);
            Thread.sleep(1000L);
            if(isCancelled())
                return null;
        }
    }
    catch(InterruptedException e)
    {
        e.getMessage();
    }
    return null;
}

private void displayImage(final Icon image)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            imageLabel.setIcon(image);
        }
    });     
}

我期望图像出现在 JLabel 中,但我只看到 JFrame 弹出。文件加载正确我已经在另一个设置中进行了测试。 有什么指点吗?

I'm trying to get a SwingWorker to work.
I've the following code at the moment:

public class ImageWorker extends SwingWorker<Void, Void> implements KeyListener
{   
private JLabel imageLabel;
private ImageIcon basicImage; 
private ImageIcon whiteImage;  
public static void main(String[] args)
{
    new ImageWorker();      
}

public ImageWorker()
{
    final JFrame frame = new JFrame();
    imageLabel = new JLabel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.getContentPane().add(imageLabel);
    frame.setVisible(true);
    try
    {
        basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
        whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
    }
    catch(IOException ex)
    {
        ex.getMessage();
    }
    this.execute();

}

@Override
protected Void doInBackground() 
{
    try
    {
        while (true)
        {
            displayImage(basicImage);
            Thread.sleep(1000L);
            if(isCancelled())
                return null;
        }
    }
    catch(InterruptedException e)
    {
        e.getMessage();
    }
    return null;
}

private void displayImage(final Icon image)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            imageLabel.setIcon(image);
        }
    });     
}

I was expecting the images to appear in the JLabel, but I only see the JFrame popping up. The files are loaded correctly Ive tested that in another setup.
Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荭秂 2024-10-27 01:00:57

这是一个使用 Timer 而不是使用 SwingWorker 的示例,这确实不适合您的情况。请注意,它与您现有的代码没有太大不同。

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class ImageWorker implements KeyListener
{
    private JLabel imageLabel;
    private ImageIcon basicImage;
    private ImageIcon whiteImage;
    private boolean isBasic =  true;
    private int delay = 1000; //milliseconds
    private Timer timer;

    public static void main(String[] args)
    {
        new ImageWorker();
    }

    public ImageWorker()
    {
        final JFrame frame = new JFrame();
        imageLabel = new JLabel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.getContentPane().add(imageLabel);
        frame.setVisible(true);

        try
        {
            basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
            whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
        }
        catch (IOException ex)
        {
            ex.getMessage();
            ex.printStackTrace();
        }

        frame.addKeyListener(this);
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(isBasic) {
                    //display basic image
                    imageLabel.setIcon(basicImage);
                }
                else {
                    //display white image
                    imageLabel.setIcon(whiteImage);
                }

                //toggle the flag
                isBasic = !isBasic;
            }
        };
        //use a timer instead of SwingWorker
        timer = new Timer(delay, taskPerformer);
        timer.start();

    }



    @Override
    public void keyPressed(KeyEvent e)
    {
        //key pressed, we want to stop toggling so stop the timer
        timer.stop();
        //do whatever else you were doing to set the value for isCancelled();
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }
}

Here is an example using a Timer rather than using the SwingWorker which really isn't appropriate to your situation. Note that it's not too different from your existing code.

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class ImageWorker implements KeyListener
{
    private JLabel imageLabel;
    private ImageIcon basicImage;
    private ImageIcon whiteImage;
    private boolean isBasic =  true;
    private int delay = 1000; //milliseconds
    private Timer timer;

    public static void main(String[] args)
    {
        new ImageWorker();
    }

    public ImageWorker()
    {
        final JFrame frame = new JFrame();
        imageLabel = new JLabel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.getContentPane().add(imageLabel);
        frame.setVisible(true);

        try
        {
            basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
            whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
        }
        catch (IOException ex)
        {
            ex.getMessage();
            ex.printStackTrace();
        }

        frame.addKeyListener(this);
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(isBasic) {
                    //display basic image
                    imageLabel.setIcon(basicImage);
                }
                else {
                    //display white image
                    imageLabel.setIcon(whiteImage);
                }

                //toggle the flag
                isBasic = !isBasic;
            }
        };
        //use a timer instead of SwingWorker
        timer = new Timer(delay, taskPerformer);
        timer.start();

    }



    @Override
    public void keyPressed(KeyEvent e)
    {
        //key pressed, we want to stop toggling so stop the timer
        timer.stop();
        //do whatever else you were doing to set the value for isCancelled();
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e)
    {
        // TODO Auto-generated method stub

    }
}
川水往事 2024-10-27 01:00:57

SwingWorker 不适合您的情况,请查看 swing 包中的 Timer 。以下是 API 的链接: http://download .oracle.com/javase/6/docs/api/javax/swing/Timer.html

您让计时器运行并每秒更改图像,因为这就是您所需要的。

另外,每当出现异常时,至少打印出堆栈跟踪或消息。否则你将不知道异常是否发生并被捕获。

A SwingWorker is not appropriate for your situation look into a Timer in the swing package. Here is a link to the API: http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html

You have the timer run and change an image every second since that is what you need.

Also, whenever you have exceptions, print out a stacktrace or the message at least. Otherwise you won't know if an exception occurs and is caught.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文