import java.awt.*;
import javax.swing.*;
public class ImageJPanel extends JPanel{
private Image image;
public ImageJPanel(Image image){ //首先构建一个构造方法.传入的参数是Image的文件路径
this.image=image;
Dimension size = new Dimension(image.getWidth(null),
image.getHeight(null));
setSize(size); //设置JPanel的大小为Image图象的大小
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null); //用G 把Image画出来
}
public static void main(String[]args){
ImageJPanel imageJP = new ImageJPanel(new ImageIcon("Image/***.jpg").getImage());
JFrame jf = new JFrame();
jf.add(imageJP);
jf.setSize(imageJP.getWidth(),imageJP.getHeight()+35);//这里+35 是因为JFrame上会有个标题栏 他会占35像素
jf.setVisible(true);
}
}
import javax.swing.*;
public class ImageJPanel extends JPanel{
private Image image;
public ImageJPanel(Image image){ //首先构建一个构造方法.传入的参数是Image的文件路径
this.image=image;
Dimension size = new Dimension(image.getWidth(null),
image.getHeight(null));
setSize(size); //设置JPanel的大小为Image图象的大小
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null); //用G 把Image画出来
}
public static void main(String[]args){
ImageJPanel imageJP = new ImageJPanel(new ImageIcon("Image/***.jpg").getImage());
JFrame jf = new JFrame();
jf.add(imageJP);
jf.setSize(imageJP.getWidth(),imageJP.getHeight()+35);//这里+35 是因为JFrame上会有个标题栏 他会占35像素
jf.setVisible(true);
}
}