JFrame中用循环控制JLabel加载图片导致图片丢失,这个问题有解决办法吗?

正常代码段:

    private void newJLabel() {
        JLabel[] thum=new JLabel[12];
        int line=1;
        for(int i=1;i<=12;i++) {
            thum[i-1]=new JLabel();
            if(i%5==1) {
                if(line==1) {
                    thum[i-1].setBounds(75*1, 50*line, 196, 282);
                }else{
                    thum[i-1].setBounds(75*1, 50*line+282*(line-1), 196, 282);
                }   
            }else if(i%5==2){
                if(line==1) {
                    thum[i-1].setBounds(75*2+(296*1), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*2+(296*1), 50*line+282*(line-1), 196, 282);
                }

            }else if(i%5==3){
                if(line==1) {
                    thum[i-1].setBounds(75*3+(296*2), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*3+(296*2), 50*line+282*(line-1), 196, 282);
                }

            }else if(i%5==4){
                if(line==1) {
                    thum[i-1].setBounds(75*4+(296*3), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*4+(296*3), 50*line+282*(line-1), 196, 282);
                }
            }else if(i%5==0){
                if(line==1) {
                    thum[i-1].setBounds(75*5+(296*4), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*5+(296*4), 50*line+282*(line-1), 196, 282);
                }

            }
            if(i%5==0) {
                line++;
            }
            thum[i-1].setOpaque(true);
            thum[i-1].setIcon(new ImageIcon("src/thumbnails/0.jpg"));//问题在这里
            thum[i-1].setVerticalAlignment(JLabel.TOP);
            this.add(thum[i-1]);
        }
    }

zheng'chang'xian'shi'tu
图片说明

异常代码段:

    private void newJLabel() {
        JLabel[] thum=new JLabel[12];
        int line=1;
        for(int i=1;i<=12;i++) {
            thum[i-1]=new JLabel();
            if(i%5==1) {
                if(line==1) {
                    thum[i-1].setBounds(75*1, 50*line, 196, 282);
                }else{
                    thum[i-1].setBounds(75*1, 50*line+282*(line-1), 196, 282);
                }   
            }else if(i%5==2){
                if(line==1) {
                    thum[i-1].setBounds(75*2+(296*1), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*2+(296*1), 50*line+282*(line-1), 196, 282);
                }

            }else if(i%5==3){
                if(line==1) {
                    thum[i-1].setBounds(75*3+(296*2), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*3+(296*2), 50*line+282*(line-1), 196, 282);
                }

            }else if(i%5==4){
                if(line==1) {
                    thum[i-1].setBounds(75*4+(296*3), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*4+(296*3), 50*line+282*(line-1), 196, 282);
                }
            }else if(i%5==0){
                if(line==1) {
                    thum[i-1].setBounds(75*5+(296*4), 50*line, 196, 282);
                }else {
                    thum[i-1].setBounds(75*5+(296*4), 50*line+282*(line-1), 196, 282);
                }

            }
            if(i%5==0) {
                line++;
            }
            thum[i-1].setOpaque(true);
            thum[i-1].setIcon(new ImageIcon("src/thumbnails/"+i+".jpg"));//问题在这里
            thum[i-1].setVerticalAlignment(JLabel.TOP);
            this.add(thum[i-1]);
        }
    }

异常显示图
图片说明

https://blog.csdn.net/weixin_30845171/article/details/97482104

你把图片的JLabel添加在了this里面,检查下自己的this的布局方式。要用setBounds方法,容器的布局方式必须是null。
代码如下


this.setLayout(null);