ch_eers 2019-11-18 09:20 采纳率: 0%
浏览 107

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]);
        }
    }

异常显示图
图片说明

  • 写回答

2条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名