日期:2014-05-20 浏览次数:20946 次
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import javax.imageio.stream.*;
import java.awt.image.*;
public class TestJPanel extends JPanel {
JFrame f = new JFrame("TestJPanel");
public void launchFrame() {
f.setLayout(new GridLayout());
//当我设置JFrame的Layout为空的时候,paintComponent方法中的图片就画不出来
//f.setLayout(null);
f.setVisible(true);
f.setBounds(100,100,300,300);
f.add(this);
//setLayout(null);
this.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//g.setColor(Color.red);
//g.fillOval(100,100,100,100);
try {
File f = new File("C:\\Users\\Forever\\Desktop\\javaProgram\\javaWuZiQi\\Background.jpg");
FileImageInputStream fiis = new FileImageInputStream(f);
BufferedImage bi = ImageIO.read(fiis);
g.drawImage(bi,0,0,null);
} catch(IOException e) {
}
}
public static void main(String[] args) {
new TestJPanel().launchFrame();
}
}