且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JPanel上添加.GIF时显示的黑色方块

更新时间:2023-12-05 08:47:34

你的问题在这里.. 。

Your problem is here...

p2.setBackground(new Color(0,0,0,150));

Swing不支持基于alpha的背景,要么你的组件是透明的,要么不是。

Swing does not support alpha based backgounds, either your component is transparent or it's not.

这样做意味着组件尝试使用alpha值作为背景填充颜色,但是油漆管理器不知道它应该在组件下面绘制,导致各种各样的问题和问题

Doing this means that the component "attempts" to use the alpha value as the background fill color, but the paint manager doesn't know it should paint beneath the component, causing all sorts of problems and issues

现在,这有点棘手。您需要使用 setOpaque(false)使容器透明,但现在这意味着背景未绘制。

Now, this a little tricky. You need to make the container transparent by using setOpaque(false), but this now means that the background is not painted.

您需要做的是创建一个自定义组件,将其 opaque 属性设置为 false 并覆盖它 paintComponent 方法并使用基于alpha的颜色填充背景。我通常喜欢使用 AlphaComposite ,但这也可以... ...

What you need to do is create a custom component, set it's opaque property to false and override it's paintComponent method and fill the background with your alpha based color. I normally like using a AlphaComposite, but this works as wel...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class TranslucentPanelExample {

    public static void main(String[] args) {
        new TranslucentPanelExample();
    }

    public TranslucentPanelExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    JLabel background = new JLabel(
                            new ImageIcon(ImageIO.read(
                                            getClass().getResource("/background.jpg"))));
                    background.setLayout(new GridBagLayout());
                    background.add(new WaitPane());

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(background);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    public class WaitPane extends JPanel {

        public WaitPane() {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(12, 12, 12, 12));
            // This is very important
            setOpaque(false);
            setBackground(new Color(0, 0, 0, 150));

            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.setHorizontalAlignment(JLabel.CENTER);
            loadBar.setVerticalAlignment(JLabel.CENTER);
            add(loadBar);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }

    }

}

布局管理器,布局管理器,布局管理器...

Layout managers, layout managers, layout managers...

我不能强调布局管理器的重要性。你依赖的是魔法数字,这些数字可能并不总是符合现实......

I can't stress enough how important layout managers are. You are relying on "magic" numbers which may not always meet reality...