Java - set opacity in JPanel

前端 未结 6 711
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:16

Let\'s say I want to make the opacity of a JPanel %20 viewable? I don\'t mean setOpaque (draw or not draw) or setVisible (show or hide)... I mean make it see-through JPanel.

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 10:20

    If you have a custom panel and want the whole thing to be semi-transparent, I advise you to do override its method paintComponent like this :

    @Override
        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            Graphics2D g2d = (Graphics2D) graphics;
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        }
    

提交回复
热议问题