【问题标题】:Button dissapears and a photo appears on its place按钮消失,照片出现在其位置
【发布时间】:2015-12-26 19:12:44
【问题描述】:

我已经创建了 JButtons,我希望当我点击它们消失时,一张 照片 占据其空白区域并 揭示。 这是我的记忆卡游戏的一部分,但我找不到在代码中制作它的方法。这是我的按钮监听器到现在为止所做的事情

private class Disappear implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
     int count =0;
        for(int i=0; i<52;i++)
        {
         if(!buttons[i].isVisible())
         {
             count+=1;
         }
        }
        if(count<2)
       ((JButton)e.getSource()).setVisible(false);

        if(count==2)
        {
           for(int i=0; i<52;i++) 
           {
               if(!buttons[i].isVisible())
               {
                   buttons[i].setVisible(true);
               }   
           }
        }
    }
}

【问题讨论】:

    标签: java jbutton photo


    【解决方案1】:

    您可以尝试使用 JToggleButton 而不是常规按钮。每当按下 JToggleButton 时,显示不同的/新图像:

    class MemGame implements ActionListener(){  
    
       ImageIcon img  = new ImageIcon("Back.png"); /* Back of the card */  
       ImageIcon img2 = new ImageIcon("D1.png");   /* Card face */  
    
       /* Constructor */
       MemGame(){  
          JFrame jfrm = new JFrame("Memory Game");
          jfrm.setSize(220, 250);
          jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jfrm.setLayout(new FlowLayout());  
    
          JToggleButton jt1 = new JToggleButton(img);  
          jt1.addActionListener(this);  
    
          jfrm.getContentPane().add(new JLabel("Select a card"));  
          jfrm.getContentPane().add(jt1);  
          jfrm.setVisible(true);  
       }  
    
       public void actionPerformed(ActionEvent ae){  
          JToggleButton jt = (JToggleButton)ae.getSource();  
          if (!jt.isSelected()) jt.setIcon(img);  
          else jt.setIcon(img2);  
       }
    }  
    

    从这里只需添加处理多个按下的卡片等的逻辑。


    结果
    Before button is pressed
    After button is pressed


    贷记到期的贷记: 卡片图片来自:whttps://thenounproject.com/term/ace-of-diamonds/170620/ 和 whttps://thenounproject.com/term/playing-card/146000/

    【讨论】:

      【解决方案2】:
         for(int i=0; i<52;i++)
          {
           if(!buttons[i].isVisible())
           {
               count+=1;
           }
          }
          if(count<2)
         ((JButton)e.getSource()).setVisible(false);
      

      这个逻辑说“如果零个或一个按钮不可见,则使该按钮不可见”。这是您想要实现的目标吗? - 好像没有。

      然后下面的循环说“如果恰好发现两个按钮不可见,则将所有不可见按钮变为可见” - 这是你的逻辑

      让“这个”按钮不可见很简单

       public void actionPerformed(ActionEvent e)
      {
      JButton jb=(JButton)e.getSource();
           if(jb.isVisible()) 
         jb.setVisible(false);
      }
      

      就够了。现在进一步使用卡片,您可能希望将图像附加到按钮并相应地显示/隐藏它。要将图标设置为空白,您可以这样做

            ((JB)e.getSource()).setIcon(null);
      

      进一步了解图像图标

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多