java 显示透明背景png图片
首先理由ps生成一个背景透明的png图片,然后设置JPanel面板的透明属性,也就是panel.setOpaque(false);设置为透明
class MyPanel extends JLayeredPane{ public myPanelOne panel = new myPanelOne();
public MyPanel(){ setPreferredSize(new Dimension(600, 600)); setLayout(null); panel.setBackground(Color.yellow); setOpaque(true); setBackground(Color.BLACK); panel.setOpaque(false); add(panel); panel.setBounds(0, 0, 200, 200); setLayer(panel, 1);//设置组建的层次 } }
class myPanelOne extends JPanel{ protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(new ImageIcon("草地.png").getImage(), 0, 0, 200, 200, this); } }
本文来自博客园,作者:hjzqyx,转载请注明原文链接:https://www.cnblogs.com/hujunzheng/p/4237753.html