2015-04-01 40 views
1

嘿家伙我试图做一个匹配卡的游戏。 如果两张牌匹配用户获取一个点和卡保持可见,否则翻转它们(或setText(“”))我做了关于摆动睡眠的研究,但我不知道如何在我的代码中实现它。我尝试了一切,但我无法实现它的工作。我有这个代码主要运行。如何睡一个按钮setText?

ActionListener buttonListener = new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      JButton selectedButton = (JButton)e.getSource(); 

      for (int row = 0; row < 6;row++){ 
        for(int col = 0; col < 6; col++){ 
         if (buttons[row][col] == selectedButton){ 
          flipCard(row, col); 
          if(stack.empty()){ 
           stack.push(row+","+col); 
          }else{ 
           String word = (String)stack.pop(); 
           String[] ar = word.split(","); 
           System.out.println(ar[0] + " " + ar[1]); 
           if (cardList.getCardNode(row, col).getLetter() == 
             cardList.getCardNode(Integer.parseInt(ar[0]), 
             Integer.parseInt(ar[1])).getLetter()){ 
            System.out.println("equal"); 
           }else{ 
            System.out.println("not equal"); 
            //Compiler complains 
            //Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.Timer cannot be cast to javax.swing.JButton 
            Timer timer = new Timer(100 ,this); 
            timer.setRepeats(false); 
            timer.start(); 
            buttons[row][col].setText(""); 
            buttons[Integer.parseInt(ar[0])] 
              [Integer.parseInt(ar[1])].setText(""); 
           } 
          } 
         } 
        } 
      } 
     } 
    }; 
+0

是什么一个'taskPerformer'? NVM - 为了尽快提供更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整可验证示例)或[SSCCE](http://www.sscce.org/)(简称,自包含,正确的例子)。此外,源代码中的单个空白行是所有* *所需的。 '{'之后或'}'之前的空行通常也是多余的。 – 2015-04-01 04:50:13

+0

*“//编译器抱怨”* - 它说什么?你的意思是像[这](http://stackoverflow.com/questions/16292498/swingworker-thread-sleep-or-javax-swing-timer-i-need-to-insert-a-pause/16293498# 16293498)? – MadProgrammer 2015-04-01 04:51:11

+0

现在我想看到一个[runnable示例](https://stackoverflow.com/help/mcve),它演示了您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混淆和更好的回应 – MadProgrammer 2015-04-01 05:18:37

回答

1

我“想”什么,你应该做的是更多的东西一样......

Timer timer = new Timer(100, new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent evt) { 
     //Pop stack coordinates and set them back to "" 
     //setText on button clicked to "" 
     System.out.println(cardList.getCardNode(row, col).getLetter());   
    } 
}); 
timer.setRepeats(false); 
timer.start(); 

这将,100毫秒的延迟之后,调用ActionListeneractionPerformed方法,可以让你重置UI的状态...

问题是我是循环内,并且被点击时只以ROW和COL访问

然后创建一个ActionListener内搭在其所需的信息和行为时actionPerformed方法被调用......

public class FlipperHandler implements ActionListener { 
    private JButton[] buttons; 
    private int[] card1, card2; 

    public FlipperHandler(JButton[] buttons, int[] card1, int[] card2) { 
     this.buttons = buttons; 
     this.card1 = card1; 
     this.card2 = card2; 
    } 

    @Override 
    public void actionPerformed(ActionEvent evt) { 
     buttons[card1[0]][card1[1]].setText(""); 
     buttons[card2[0]][card2[2]].setText("");  
    } 
} 

然后与Timer使用它...

Timer timer = new Timer(100, new FlipperHandler(buttons, 
             new int[]{row, col}, 
             new int[]{Integer.parseInt(ar[0]), Integer.parseInt(ar[1])}); 
timer.setRepeats(false); 
timer.start(); 
+0

问题是我在循环内部,并且只有在点击时才能访问行和列。 – Beto 2015-04-01 05:18:49

+0

制作一个'ActionListener'类,它将需要的信息用于重置状态 – MadProgrammer 2015-04-01 05:20:22

+0

@Beto Check updaye ... – MadProgrammer 2015-04-01 05:40:55