JButton JLabel等等都可以直接加背景图片..
以前自己都用一种笨拙的方法 就是将图片加在JLabel上.然后在加在 JPanel =.= 经过不段的学习..
终于晓得如何用简单的办法去画JPanel背景
图片预览
代码:
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author chensiyu04
* @address www.blogjava.net/chensiyu04
*/
public class ImagePane extends JPanel {
private Image image;
public ImagePane() {
try {
image = ImageIO.read(new File("src/demo/java.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new ImagePane());
frame.setSize(800, 600);
frame.setVisible(true);
}
}
有问题的话可以留言..我会及时更改..