我有一个画布,当鼠标进入它时,我想要一些透明的 Jpanel,其中包含一些显示在我的画布顶部的组件。我为此使用了 JlayeredPane;但是正如您在下面的示例中看到的,当我想在画布顶部显示透明面板时,通过将其添加到上层的 jLayeredPane,它的背景显示为画布下方面板的颜色。
public class NewClass {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200 , 200);
JLayeredPane layeredPane = new JLayeredPane();
frame.setContentPane(layeredPane);
JPanel canvasPanel = new JPanel();
canvasPanel.setLayout(new GridLayout());
canvasPanel.setBackground(Color.RED);
Canvas canvas = new Canvas();
canvas.setBackground(Color.BLACK);
canvasPanel.add(canvas);
layeredPane.add(canvasPanel , JLayeredPane.PALETTE_LAYER);
canvasPanel.setSize(200 , 200);
JPanel transparentPanel = new JPanel();
transparentPanel.setSize(100 , 100);
transparentPanel .setOpaque(false);
transparentPanel.add(new JButton("button"));
layeredPane.add(transparentPanel , JLayeredPane.DRAG_LAYER);
frame.setVisible(true);
}
}
如何在画布上显示透明面板,使其背景看起来像画布?