我的问题是,当将 .GIF 添加到 JPanel 时,它会显示 .GIF 的黑色方形背景。
在JPanel上添加时的结果:
![http://i.imgur.com/W1HXgZ1.jpg](https://i.stack.imgur.com/TRzGg.jpg)
当我使用这一行时会发生这种情况:
p2.add(loadBar); // where p2 = new JPanel();
但是,当我在 JFrame 上添加相同的 .GIF 时,黑色方块不再存在。像这样:
jf.add(loadBar); // where jf = new JFrame();
添加到 JFrame 时的结果:
![http://i.imgur.com/0pMAS30.jpg](https://i.stack.imgur.com/jFsWY.jpg)
部分类代码:
String loadLink = "http://i.imgur.com/mHm6LYH.gif";
URL ajaxLoad = null;
try {
ajaxLoad = new URL(loadLink);
} catch (MalformedURLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
ImageIcon loading = new ImageIcon(ajaxLoad);
JLabel loadBar = new JLabel(loading);
loadBar.setBounds(70, 60, 54, 55);
loadBar.setOpaque(false);
//jf.add(loadBar);
p2.add(loadBar);
有人可以解释一下为什么会这样吗?感谢您的时间和阅读。
EDIT:
// Creates the Initialization Panel
p2 = new JPanel();
// Sets the background, black with 125 as alpha value
// This is less transparent
p2.setLayout(null);
p2.setBackground(new Color(0,0,0,150));
// Sets a border to the JPanel
p2.setBorder(new LineBorder(Color.WHITE));
// Sets some size to the panels
p2.setBounds(20, 20, 200, 150);
// Adds the loading gif
String loadLink = "http://i.imgur.com/mHm6LYH.gif";
URL ajaxLoad = null;
try {
ajaxLoad = new URL(loadLink);
} catch (MalformedURLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
ImageIcon loading = new ImageIcon(ajaxLoad);
JLabel loadBar = new JLabel(loading);
loadBar.setBounds(70, 60, 54, 55);
loadBar.setOpaque(false);
p2.add(loadBar);
这是第一张图片中显示的没有 JLabel 的 JPanel。我无法真正在代码中显示 JFrame 部分,因为它分布在整个类中。但我不认为问题出在 JFrame 上,所以可能是这个 JPanel :/