ImageIcon image = new ImageIcon("5.gif");
...
ImagePanel panel = new ImagePanel(new BorderLayout(),image);
...class ImagePanel extends JPanel {
private ImageIcon bg;
public ImagePanel(LayoutManager layout,ImageIcon icon) {
super(layout);
bg = icon;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = bg.getImage();
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
...
ImagePanel panel = new ImagePanel(new BorderLayout(),image);
...class ImagePanel extends JPanel {
private ImageIcon bg;
public ImagePanel(LayoutManager layout,ImageIcon icon) {
super(layout);
bg = icon;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = bg.getImage();
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
我把ImageIcon image = new ImageIcon("5.gif");
加到了setSize(WIDTH, HEIGHT);后面不知道对不对?
你用的是5.jpg?
5.gif必须在classpath下面
setSize不用了,BorderLayout会帮你拉扯图片大小的了