首页 新闻 赞助 找找看

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

0
悬赏园豆:50 [已解决问题] 解决于 2019-11-21 11:10

正常代码段:

 

 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     }

异常显示:

技术无罪的主页 技术无罪 | 初学一级 | 园豆:-82
提问于:2019-11-18 09:35

出现这个问题是因为加了这个:setOpaque(true);注释掉就可以了

技术无罪 4年前
< >
分享
最佳答案
0

出现这个问题是因为加了这个:setOpaque(true);注释掉就可以了

技术无罪 | 初学一级 |园豆:-82 | 2019-11-21 11:10
其他回答(1)
0

是不是你图片大小不一样

收获园豆:50
yytxdy | 园豆:1680 (小虾三级) | 2019-11-18 11:21

图片都是192*282的,一样大

支持(0) 反对(0) 技术无罪 | 园豆:-82 (初学一级) | 2019-11-20 08:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册