首先理由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); } }