我正在尝试制作一款游戏,每当播放某张卡片时,透明 GIF 就会弹出并播放,但每当它出现时,都会出现一个黑色矩形,然后再次变得透明并播放 GIF。它不会影响任何功能,但看起来有点烦人。
这是我的初始代码,创建一个创建面板的线程,播放 GIF,然后销毁面板:
public class UnoGUI{
public static void main(String[] args) {
// Makes a frame without borders
JFrame pop = new JFrame();
pop.setUndecorated(true);
// Makes the Frame Itself Transparent.
pop.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
// Makes a Square
pop.setSize(230, 350);
// Puts it in the Middle
pop.setLocation(550, 250);
// TEST
JLabel label = new JLabel(); // Test
// Gets the GIF (Black Square Occurs regardless of if it is a GIF or an Image)
String arg = "coffee.png"; // special_gifs/Blue_Skip.gif
ImageIcon icon = null;
try {
icon = new ImageIcon(arg);
}
catch (Exception e) {
System.out.println("File Not Found");
}
label.setIcon(icon);
pop.getContentPane().add(label);
// END TEST
// Makes it visible
pop.setVisible(true);
// Waits three seconds
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(2);
}
catch (Exception e) {
System.out.println("Popup Failed");
}
// Destroys the popup.
pop.dispose();
}
}
我已经尝试注释掉 GIF 本身以缩小问题范围,但矩形仍然会出现一瞬间:
public class UnoGUI{
public static void main(String[] args) {
// Makes a frame without borders
JFrame pop = new JFrame();
pop.setUndecorated(true);
// Makes the Frame Itself Transparent.
pop.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
// Makes a Square
pop.setSize(230, 350);
// Puts it in the Middle
pop.setLocation(550, 250);
// TEST
JLabel label = new JLabel (); // Test
/*
// COMMENTED OUT
// Gets the GIF(Black Square Occurs regardless of if it is a GIF or an Image)
String arg = "special_gifs/Blue_Skip.gif";
ImageIcon icon = null;
try {
icon = new ImageIcon (arg);
} catch (Exception e) {
System.out.println("File Not Found");
}
label.setIcon(icon);
// END COMMENT
*/
pop.getContentPane().add(label);
// END TEST
// Makes it visible
pop.setVisible(true);
// Waits three seconds
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
System.out.println("Popup Failed");
}
// Destroys the popup.
pop.dispose();
}
}
但是当我注释掉整个标签时,黑色矩形在程序运行时消失了:
public class UnoGUI{
public static void main(String[] args) {
// Makes a frame without borders
JFrame pop = new JFrame();
pop.setUndecorated(true);
// Makes the Frame Itself Transparent.
pop.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
// Makes a Square
pop.setSize(230, 350);
// Puts it in the Middle
pop.setLocation(550, 250);
/*
// COMMENTED OUT
// TEST
JLabel label = new JLabel (); // Test
// Gets the GIF(Black Square Occurs regardless of if it is a GIF or an Image)
String arg = "special_gifs/Blue_Skip.gif";
ImageIcon icon = null;
try {
icon = new ImageIcon (arg);
} catch (Exception e) {
System.out.println("File Not Found");
}
label.setIcon(icon);
pop.getContentPane().add(label);
// END COMMENT
*/
// END TEST
// Makes it visible
pop.setVisible(true);
// Waits three seconds
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
System.out.println("Popup Failed");
}
// Destroys the popup.
pop.dispose();
}
}
我认为这可能与标签加载有关,因为它只存在一秒钟,并且只有在标签被移除时才会消失,但我想知道是否有办法让它消失,这样它一直是透明的。