How to set a transparent background of JPanel?

后端 未结 7 1803
栀梦
栀梦 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:25

    To set transparent you can set opaque of panel to false like

       JPanel panel = new JPanel();
       panel.setOpaque(false);
    

    But to make it transculent use alpha property of color attribute like

       JPanel panel = new JPanel();
       panel.setBackground(new Color(0,0,0,125));
    

    where last parameter of Color is for alpha and alpha value ranges between 0 and 255 where 0 is full transparent and 255 is fully opaque

提交回复
热议问题