向JPanel中添加图片

来源:互联网 发布:网络推广教学视频 编辑:程序博客网 时间:2024/05/02 04:31
import java.awt.*;
import javax.swing.*;


public class PicDemo extends JFrame {

private JPanel main,p;
private Icon icon;
private JLabel l;

private PicDemo(){

main = new JPanel();
p = new JPanel();

l=new JLabel();
icon=new ImageIcon("C:\\Users\\Administrator\\Pictures\\cat.jpg");     //在此直接创建对象,注意目录之间的分隔符是双斜线
l.setIcon(icon);
l.setBounds(10, 10, icon.getIconWidth(),icon.getIconHeight());
p.add(l,new Integer(Integer.MIN_VALUE));

this.setVisible(true);
this.add(main);
main.add(p);
this.pack();
}
public static void main(String[] args) {
new PicDemo();
}

}

组件由小到大分别添加次序:ImageIcon icon>JLabel l>JPanel p(p可以是最后一个面板,也可以将p再继续添加到其他面板中)

图片参数设置:setBounds(int x,int y,int width, int height)// x,y是组件左上角在容器中的坐标;width,height是组件的宽度和高度

0 0
原创粉丝点击