BufferedImage显示在JPanel上

import java.awt.Graphics;
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 Test1 {
	public static void main(String[] args) {
		JFrame jframe = new JFrame();
		jframe.add(new ImagePanel());
		jframe.setSize(400, 300);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
	}
}

class ImagePanel extends JPanel {

	private BufferedImage image;

	public ImagePanel() {
		try {
			image = ImageIO.read(new File("player1.png"));
		} catch (IOException ex) {
			// handle exception...
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(image, 0, 0, null); 
	}

}
 

你可能感兴趣的:(java,swing)