ITPub博客

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

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

作者:netroop 时间:2019-11-18 12:00:03 0 删除 编辑
1     private void newJLabel() {
 2         JLabel[] thum=new JLabel[12];
 3         int line=1;
 4         for(int i=1;i<=12;i++) {
 5             thum[i-1]=new JLabel();
 6             if(i%5==1) {
 7                 if(line==1) {
 8                     thum[i-1].setBounds(75*1, 50*line, 196, 282);
 9                 }else{
10                     thum[i-1].setBounds(75*1, 50*line+282*(line-1), 196, 282);
11                 }    
12             }else if(i%5==2){
13                 if(line==1) {
14                     thum[i-1].setBounds(75*2+(296*1), 50*line, 196, 282);
15                 }else {
16                     thum[i-1].setBounds(75*2+(296*1), 50*line+282*(line-1), 196, 282);
17                 }
18                 
19             }else if(i%5==3){
20                 if(line==1) {
21                     thum[i-1].setBounds(75*3+(296*2), 50*line, 196, 282);
22                 }else {
23                     thum[i-1].setBounds(75*3+(296*2), 50*line+282*(line-1), 196, 282);
24                 }
25                 
26             }else if(i%5==4){
27                 if(line==1) {
28                     thum[i-1].setBounds(75*4+(296*3), 50*line, 196, 282);
29                 }else {
30                     thum[i-1].setBounds(75*4+(296*3), 50*line+282*(line-1), 196, 282);
31                 }
32             }else if(i%5==0){
33                 if(line==1) {
34                     thum[i-1].setBounds(75*5+(296*4), 50*line, 196, 282);
35                 }else {
36                     thum[i-1].setBounds(75*5+(296*4), 50*line+282*(line-1), 196, 282);
37                 }
38                 
39             }
40             if(i%5==0) {
41                 line++;
42             }
43             thum[i-1].setOpaque(true);
44             thum[i-1].setIcon(new ImageIcon("src/thumbnails/3.jpg"));//问题出在这里
45             thum[i-1].setVerticalAlignment(JLabel.TOP);
46             this.add(thum[i-1]);
47         }
48     }

正常显示:


异常代码段:

1     private void newJLabel() {
 2         JLabel[] thum=new JLabel[12];
 3         int line=1;
 4         for(int i=1;i<=12;i++) {
 5             thum[i-1]=new JLabel();
 6             if(i%5==1) {
 7                 if(line==1) {
 8                     thum[i-1].setBounds(75*1, 50*line, 196, 282);
 9                 }else{
10                     thum[i-1].setBounds(75*1, 50*line+282*(line-1), 196, 282);
11                 }    
12             }else if(i%5==2){
13                 if(line==1) {
14                     thum[i-1].setBounds(75*2+(296*1), 50*line, 196, 282);
15                 }else {
16                     thum[i-1].setBounds(75*2+(296*1), 50*line+282*(line-1), 196, 282);
17                 }
18                 
19             }else if(i%5==3){
20                 if(line==1) {
21                     thum[i-1].setBounds(75*3+(296*2), 50*line, 196, 282);
22                 }else {
23                     thum[i-1].setBounds(75*3+(296*2), 50*line+282*(line-1), 196, 282);
24                 }
25                 
26             }else if(i%5==4){
27                 if(line==1) {
28                     thum[i-1].setBounds(75*4+(296*3), 50*line, 196, 282);
29                 }else {
30                     thum[i-1].setBounds(75*4+(296*3), 50*line+282*(line-1), 196, 282);
31                 }
32             }else if(i%5==0){
33                 if(line==1) {
34                     thum[i-1].setBounds(75*5+(296*4), 50*line, 196, 282);
35                 }else {
36                     thum[i-1].setBounds(75*5+(296*4), 50*line+282*(line-1), 196, 282);
37                 }
38                 
39             }
40             if(i%5==0) {
41                 line++;
42             }
43             thum[i-1].setOpaque(true);
44             thum[i-1].setIcon(new ImageIcon("src/thumbnails/"+i+".jpg"));//这里改成i就出毛病了
45             thum[i-1].setVerticalAlignment(JLabel.TOP);
46             this.add(thum[i-1]);
47         }
48     }


异常显示:

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4741/viewspace-3165195/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    152
  • 访问量
    1069