透明JPanel over Canvas(VLCJ)
我知道之前发布了类似的问题,但没有答案或示例代码。
我需要一个透明的JPanel在画布上。下面发布的代码不起作用
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属性是最后的解决方案(非常不鼓励)
没有找到相关结果
已邀请:
2 个回复
梆晨灸碾
泪琉踞檄