キャンバスがあり、マウスがキャンバスに入ったときに、キャンバスの上に表示されるいくつかのコンポーネントを含む透明な 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);
}
}
背景がキャンバスのように見えるように、キャンバスに透明なパネルを表示するにはどうすればよいですか?