透明JPanel over Canvas(VLCJ) [英] Transparent JPanel over Canvas (VLCJ)

查看:279
本文介绍了透明JPanel over Canvas(VLCJ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个类似的问题是在 a>,但没有答案或示例代码。

I know that a similar question was posted before, but there was no answer or example code.

我需要一个透明的JPanel在画布上。下面发布的代码不起作用

I need a transparent JPanel on top of a canvas. The code posted below is not working

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static class Background extends Canvas{
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.RED);
            g.drawOval(10, 10, 20, 20);
        }
    }

    private static class Transparent extends JPanel {

        public Transparent() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.GREEN);
            g.drawOval(20, 20, 20, 20);
        }
    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        JLayeredPane layered = new JLayeredPane();
        Background b = new Background();
        Transparent t = new Transparent();

        layered.setSize(200, 200);
        b.setSize(200, 200);
        t.setSize(200, 200);

        layered.setLayout(new BorderLayout());
        layered.add(b, BorderLayout.CENTER, 1);
        layered.add(t, BorderLayout.CENTER, 0);

        frame.setLayout(new BorderLayout());
        frame.add(layered, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

使用整个框架的GlassPane属性最后一个解决方案(高度不鼓励)

Using the GlassPane property of the entire frame is the very last solution (highly discouraged)

推荐答案

你可能无法使这个工作,因为你正在混合重量级和轻量级组件

You probably will not be able to get this to work because you are mixing heavyweight and lightweight components together.

在过去,它曾经是不可能在重量级组件(如Canvas)上绘制轻量级面板。由于JDK 6 Update 12和JDK 7 build 19已经更正了这一点,您可以正确地重叠2,但它有限制。具体来说,在您的情况下,重叠摇摆组件不能透明。

In the past it used to be impossible to draw lightweight panels over heavyweight components like a Canvas. Since JDK 6 Update 12 and JDK 7 build 19 Java has corrected this and you can overlap the 2 correctly however it comes with limitations. Specifically in your case the Overlapping swing component cannot be transparent.

包括新的行为的一个好的描述可以在这个页面找到:混合重量级和轻量级组件检查您的特定问题的限制部分。

A good description for this including the newer behaviour can be found on this page: Mixing Heavyweight and Lightweight Components Check the limitations section for your specific problem.

我不认为使用GlassPane会有帮助,因为它也是轻量级的。

I don't think using the GlassPane will help as it is also lightweight.

如果你改变BackGround类来扩展JPanel的画布,你会得到你想要的行为。

If you change the BackGround class to extend JPanel instead of Canvas you will get the behaviour you want.

这篇关于透明JPanel over Canvas(VLCJ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆