kotlin 无法在JPanel上的另一个非不透明JPanel后面看到JLabel(OverlayLayout)

ee7vknir  于 2023-08-07  发布在  Kotlin
关注(0)|答案(1)|浏览(112)

我想标签 “测试!“ 坐在黑盒子后面,并且能够看到后面的背景图像。我该如何实现这一点?
这是我的真实的问题的一个最小示例,我试图将2个框架大小的JPanel分层,以便在我的应用程序上创建一个导航覆盖。

private fun createAndShowGUI() {
        defaultCloseOperation = EXIT_ON_CLOSE

        contentPane = JLabel(ImageIcon("C:\\...\\bee.jpg"))
        layout = FlowLayout()

        //
        val translucentPanel: JPanel = object: JPanel() {
            override fun paintComponent(g: Graphics?) {
                super.paintComponent(g)

                val graphics = g!!.create() as Graphics2D
                graphics.composite = AlphaComposite.SrcOver.derive(0.5f)
                graphics.color = background
                graphics.fillRect(0, 0, width, height)
                graphics.dispose()
            }
        }
        translucentPanel.background = Color(0, 0, 0, 125)
        translucentPanel.preferredSize = Dimension(250, 150)
        translucentPanel.isOpaque = false

        //
        val backingPanel = JPanel()
        backingPanel.isOpaque = false
        backingPanel.layout = OverlayLayout(backingPanel)

        backingPanel.add(translucentPanel)
        backingPanel.add(Label("Test!"))

        //
        add(backingPanel)

        setSize(600, 400)
        isVisible = true
    }

字符串
x1c 0d1x的数据

ssm49v7z

ssm49v7z1#

Swing不支持基于Alpha的背景色。Swing组件可以是完全不透明的,也可以是完全透明的。
但是你可以假装


的数据

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.OverlayLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {
        public TestPane() {
            setLayout(new BorderLayout());
            setBackground(Color.RED);
            setBorder(new EmptyBorder(32, 32, 32, 32));

            JPanel overlayPane = new JPanel();
            overlayPane.setOpaque(false);
            overlayPane.setLayout(new OverlayLayout(overlayPane));

            TranslucentPane translucentPane = new TranslucentPane();
            translucentPane.setAlpha(0.5f);
            translucentPane.setBackground(Color.RED);

            overlayPane.add(translucentPane);
            overlayPane.add(new JLabel("This is a test"));

            add(overlayPane);

            JSlider slider = new JSlider(0, 100);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    float alpha = slider.getValue() / 100f;
                    translucentPane.setAlpha(alpha);
                }
            });

            add(slider, BorderLayout.SOUTH);
        }
    }

    public class TranslucentPane extends JPanel {

        private float alpha = 1f;

        public TranslucentPane() {
            setOpaque(false);
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        public float getAlpha() {
            return alpha;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(getAlpha()));
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }
}

字符串
您的第一项工作是摆脱translucentPanel.background = Color(0, 0, 0, 125),而使用Color.BLACK
还有,我不得不改变组件的顺序,所以半透明窗格是先添加的,然后是标签,这样可以确保半透明窗格渲染在标签的上方

相关问题