为什么 Black Rectangle 在变透明和播放 GIF 之前出现在 JPanel 上,有没有办法摆脱它? [关闭]

问题描述 投票:0回答:0

我正在尝试制作一个游戏,每当播放某张牌时,就会弹出一个透明的 gif 并播放,但每当它出现时,都会出现一个黑色矩形,然后再次变得透明并播放 gif。它不会影响任何功能,但看起来有点烦人。

这是我的初始代码,创建一个创建面板的线程,播放 gif,然后销毁面板:

public class Popup extends Thread {
    // To determine what Image Pops Up
    private String type;

    // Thread Constructor
    public Popup(String type) {
        this.type = type;
    }

    // Shows for a couple of second before deleting itself
    public void run() {

        // 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

        // Makes Button Clear
        label.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));

        // Gets the GIF
        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 {
            sleep(5000);
        }
        catch (Exception e) {
            System.out.println("Popup Failed");
        }

        // Destroys the popup.
        pop.dispose();
    }
}

我已经尝试注释掉 gif 本身以缩小问题范围,但矩形仍然会出现一瞬间:

public class Popup extends Thread  {        
    // To determine what Image Pops Up
    private String type;

    // Thread Constructor
    public Popup (String type) {
            this.type = type;
    }
        
    // Shows for a couple of second before deleting itself
    public void run() {

        // 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

        // Makes Button Clear
        label.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));

        /*
        // COMMENTED OUT

        // Gets the GIF
        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 {
            sleep(5000);
        } catch (Exception e) {
            System.out.println("Popup Failed");
        }

        // Destroys the popup. 
        pop.dispose();
       }
}

但是当我注释掉整个标签时,黑色矩形在程序运行时消失了:

public class Popup extends Thread  {        
    // To determine what Image Pops Up
    private String type;

    // Thread Constructor
    public Popup (String type) {
            this.type = type;
    }
        
    // Shows for a couple of second before deleting itself
    public void run() {

        // 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

        // Makes Button Clear
        label.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));

        // Gets the GIF
        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 {
            sleep(5000);
        } catch (Exception e) {
            System.out.println("Popup Failed");
        }

        // Destroys the popup. 
        pop.dispose();
       }
}

我认为这可能与标签加载有关,因为它只存在一秒钟,并且只有在标签被移除时才会消失,但我想知道是否有办法让它消失,这样它一直是透明的。

谢谢你的帮助!

java multithreading swing transparency jlabel
© www.soinside.com 2019 - 2024. All rights reserved.