23,407
社区成员
发帖
与我相关
我的任务
分享package com.gloomyfish.swing;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestJPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// private static Graphics g; very bad way!!!
private BufferedImage bi;
public TestJPanel() throws IOException // throws IOException
{
File file = new File("D:\\share_folder\\snow2006.jpg");
bi = null;
try {
bi = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
TestJPanel s = new TestJPanel();
JFrame jj = new JFrame();
jj.getContentPane().add(s, BorderLayout.CENTER);
jj.setSize(300, 300);
jj.setVisible(true);
}
public void paintComponent(Graphics g) {
if(bi != null)
{
g.drawImage(bi, 0, 0, null);
}
}
}
Jframe改成Frame试试