整个applet用JPanel分成左右两个部分,我在左边设置了一幅图做为背景,但是后面添加的按钮和JLabel都被这幅图盖住了。
代码如下:
ImageIcon background;
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL url;
String str = "cards/1.jpg";
url = urlLoader.findResource(str);
background = new ImageIcon(url);//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
setLayout(null);
leftPanel.add(label,new Integer(Integer.MIN_VALUE));
label.setSize(background.getIconWidth(),background.getIconHeight());
label.setVisible(true);
代码如下:
ImageIcon background;
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL url;
String str = "cards/1.jpg";
url = urlLoader.findResource(str);
background = new ImageIcon(url);//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
setLayout(null);
leftPanel.add(label,new Integer(Integer.MIN_VALUE));
label.setSize(background.getIconWidth(),background.getIconHeight());
label.setVisible(true);
要重写一下JPanel
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (image != null) {
g2d.drawImage(image, 0, 0, this);
}
}