更改imageIcon后,JLabel不会显示

时间:2016-07-13 18:58:43

标签: java netbeans jframe jlabel imageicon

这就是我的GUI的样子。当我点击攻击按钮时,它应该看起来像这样

enter image description here

下面的2个jlabels(2个蓝色圆圈)将被替换。但在我的代码中它只是这样.. enter image description here

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
       }
   }



}                      

我尝试将jlabels的可见性设置为true但它仍然无效。有关如何做到这一点的任何想法?我们将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试在动作攻击按钮

中重绘包含这些组件的面板
jpanel.repaint();