下面这个程序,我把JPanel的布局设置为null,然后在paint那里画了一张背景图,可是我添加的JLabel到这个JPanel里面却看不到,麻烦高人帮我看看是怎么一回事
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ChessTest extends JFrame {
public ChessTest() {
MyPanel p = new MyPanel();
p.setPreferredSize(new Dimension(558, 620));
JLabel l = new JLabel();
l.setIcon(new ImageIcon(System.getProperty("user.dir") + "/chessimg/红车.png"));
l.setBounds(100, 100, 55, 55);
p.add(l); add(p); pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new ChessTest();
}
}
@SuppressWarnings("serial")
class MyPanel extends JPanel {
public MyPanel() {
setLayout(null);
}
@Override public void paint(Graphics g) {
super.paint(g);
BufferedImage img = null;
try {
img = ImageIO.read(new File(System.getProperty("user.dir") + "/chessimg/board.png"));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img, 0, 0, 558, 620, null);
}}
就算标签里有内容也看不到被图片遮住,图片不是被画在最下面的,怎么遮住组件呢 |