繁体   English   中英

如何休眠按钮setText?

[英]How to sleep a button setText?

大家好,我正在尝试制作匹配纸牌游戏。 如果有两张匹配的卡片,用户得到了一个点并且卡片仍然可见,否则翻转它们(或setText(“”)),我对秋千睡眠进行了研究,但不确定如何在代码中实现它。 我已经尝试了所有方法,但是无法正常工作。 我有此代码在main中运行。

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("");
                                }
                            }
                        }
                    }
            }
        }
    };

我“认为”您应该做的事情更像是...

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();

暂无
暂无

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

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