向JPanel中添加图片

    xiaoxiao2021-03-25  79

    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是组件的宽度和高度

    转载请注明原文地址: https://ju.6miu.com/read-38624.html

    最新回复(0)