我正在尝试在 JFrame 中显示具有透明背景的 GIF。我的初始代码很简单。
import java.awt.*;
import javax.swing.*;
public class GUIProject
{
public static void main(String [] args)
{
JFrame pokemonFrame = new JFrame();
pokemonFrame.setSize(800, 800);
ImageIcon imageLabel = new ImageIcon("/Users/Hannah/eclipse-workspace/Graphics/pokemon images/pikasprite.gif");
JLabel label = new JLabel(imageLabel);
pokemonFrame.setLayout(new FlowLayout());
pokemonFrame.add(label);
pokemonFrame.setVisible(true);
}
}
不幸的是,我遇到了与 this post 完全相同的问题需要。本质上,当 gif 循环播放时,过去的 gif 帧会保留在屏幕上,从而产生一种重影效果。我使用的图像与我链接的帖子使用的图像完全相同。我尝试实现所选答案给出的解决方案,但它对我不起作用,可能是因为它太高级了,我无法理解。
我只是一个初学者,但我真的很想为我正在为类(class)进行的项目解决这个问题。
我需要一些帮助来实现此代码或找到我遇到的问题的另一种解决方案。 (如果这个问题不恰当,我很抱歉!我自己从未使用过 Stack Overflow,但我以前潜伏过。)
最佳答案
这是我使用的代码:
import java.awt.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
JLabel label = new JLabel( new ImageIcon("bart_simpson.gif") );
add( label );
setBackground( Color.YELLOW );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
// frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
这是我使用的图像:
我相信这是一个透明图像,因为您可以更改面板的背景并且背景通过图像显示。
关于java - 在 JFrame 中显示透明 GIF,而不在播放时保留每一帧? (初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56519176/