简体   繁体   English

更改imageIcon后JLabel不显示

[英]JLabel does not show up after changing imageIcon

this is how my GUI looks like. 这就是我的GUI外观。 When I click the attack button it should look like this 当我单击“攻击”按钮时,它应如下所示

在此处输入图片说明

the 2 jlabels below(the 2 blue circles) will be replaced. 下面的2个jlabel(2个蓝色圆圈)将被替换。 but in my code it only looks like this.. 但是在我的代码中,它看起来只有这样。 在此处输入图片说明

private static String[] imageList = {"/com.baylon.img/rock.png","/com.baylon.img/paper.png","/com.baylon.img/scissors.png"};

private void btnAttackActionPerformed(java.awt.event.ActionEvent evt) {                                          
   int compChoice = (int)(Math.random()*3);
   int playerChoice =  getVal((String)cboxPlayerChoice.getSelectedItem());
   int numRounds = Integer.parseInt(txtNumRounds.getText());
    ImageIcon rock = new ImageIcon(imageList[0]);
     ImageIcon paper = new ImageIcon(imageList[1]);
      ImageIcon scissors = new ImageIcon(imageList[2]);


   if(playerChoice == 0 ){
       lblImgPlayer.setIcon(rock);
       lblImgPlayer.setVisible(true);
      if(compChoice == 1){
          lblImgComp.setIcon(paper); //player loses
          lblImgComp.setVisible(true);
       }else if(compChoice == 2){
            lblImgComp.setIcon(scissors); 
             lblImgComp.setVisible(true);//player wins
       }else if(compChoice == 0){
            lblImgComp.setIcon(rock);
             lblImgComp.setVisible(true);//draw
       }
   }

   if(playerChoice == 1){
        lblImgPlayer.setIcon(paper);
       if(compChoice == 0){
            lblImgComp.setIcon(rock);//player wins
       }else if(compChoice == 1){
            lblImgComp.setIcon(paper);//draw
       }else if(compChoice == 2){
            lblImgComp.setIcon(scissors);//player loses
       }
   }

    if(playerChoice == 2){
        lblImgPlayer.setIcon(scissors);
       if(compChoice == 0){
           lblImgComp.setIcon(rock);//player loses
       }else if(compChoice == 1){
           lblImgComp.setIcon(paper);//player wins
       }else if(compChoice == 2){
           lblImgComp.setIcon(scissors);//draw
       }
   }



}                      

i tried setting the jlabels' visibility to true but it still doesnt work. 我尝试将jlabels的可见性设置为true,但仍然无法正常工作。 Any idea on how to do this? 关于如何执行此操作的任何想法? We'll be much appreciated. 我们将不胜感激。 Thank you! 谢谢!

尝试在动作攻击按钮中重新粉刷包含这些组件的面板

jpanel.repaint();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM