How to set a transparent background of JPanel?

后端 未结 7 1731
栀梦
栀梦 2020-12-05 04:57

Can JPanels background be set to transparent?

My frame is has two JPanels:

  • Image Panel and
相关标签:
7条回答
  • 2020-12-05 05:38
     public void paintComponent (Graphics g)
        { 
    ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.0f)); // draw transparent background
         super.paintComponent(g);
        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f)); // turn on opacity
        g.setColor(Color.RED);
        g.fillRect(20, 20, 500, 300);
         } 
    

    I have tried to do it this way, but it is very flickery

    0 讨论(0)
提交回复
热议问题