Java 刷新JFrame上的图像

Java 刷新JFrame上的图像,java,swing,awt,Java,Swing,Awt,我想用文本和图像GIF显示JLabel。当我从测试类调用showLabel()方法时,我只看到文本。我不知道如何刷新此图像以显示它。我了解到在这种情况下AWT有问题 问题:如何在我的JLabel中显示此GIF图像? 标签级 public class LabelTest{ private JWindow frame; private JLabel jLabel; ImageIcon icon = new ImageIcon("PHOTO.gif"); p

我想用文本和图像GIF显示JLabel。当我从测试类调用showLabel()方法时,我只看到文本。我不知道如何刷新此图像以显示它。我了解到在这种情况下AWT有问题

问题:如何在我的JLabel中显示此GIF图像?

标签级

public class LabelTest{

    private JWindow frame;
    private JLabel jLabel;
    ImageIcon icon = new ImageIcon("PHOTO.gif");    

    public LabelTest() {
    }

    public void showLabel(String label) {
        frame = new JWindow();
        frame.setAlwaysOnTop(true);
        frame.setBackground(new Color(255, 255, 255));
        frame.setContentPane(new TranslucentPane());
        frame.add(new JLabel(label, icon, JLabel.CENTER));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.paintAll(frame.getGraphics());
    }

    public void hideLabel() {
        frame.dispose();
    }
}
public class Test {
    public static void main(String[] args){     
        try {
            LabelTest p = new LabelTest();
            p.showLabel("I'M LABEL...");
            Thread.sleep(5000);     
            p.hideLabel();
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}
测试类

public class LabelTest{

    private JWindow frame;
    private JLabel jLabel;
    ImageIcon icon = new ImageIcon("PHOTO.gif");    

    public LabelTest() {
    }

    public void showLabel(String label) {
        frame = new JWindow();
        frame.setAlwaysOnTop(true);
        frame.setBackground(new Color(255, 255, 255));
        frame.setContentPane(new TranslucentPane());
        frame.add(new JLabel(label, icon, JLabel.CENTER));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.paintAll(frame.getGraphics());
    }

    public void hideLabel() {
        frame.dispose();
    }
}
public class Test {
    public static void main(String[] args){     
        try {
            LabelTest p = new LabelTest();
            p.showLabel("I'M LABEL...");
            Thread.sleep(5000);     
            p.hideLabel();
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}
半透明窗格

public class TranslucentPane extends JPanel {
    public TranslucentPane() {
        setOpaque(true);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
    }
}

我同意LuxxMiner的观点。
最可能的情况是,找不到图像。
要检查路径,可以使用以下检查:

public ImageIcon loadImageIconFromPath(String path) {
    URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}
因此,不是:

frame.add(new JLabel(label, icon, JLabel.CENTER));
请尝试以下操作:

ImageIcon icon = loadImageIconFromPath("PHOTO.gif");    
if (icon != null)
    frame.add(new JLabel(label, icon, JLabel.CENTER));
else 
    frame.add(new JLabel("Missing image", JLabel.CENTER));

看起来程序找不到图像;
PHOTO.gif
在哪里?如果它在项目的
src
文件夹中,请使用
getClass().getResource(“PHOTO.gif”)
。如果它不在项目中的某个地方,则必须输入图像的完整路径作为参数。(例如
c:\\temp\\PHOTO.gif
)不需要
半透明窗格类。您尚未更改JPanel的任何默认行为。默认情况下,JPanel是不透明的。摆脱那个阶级。