java - 如何让按钮setText hibernate ?

标签 java swing actionlistener

嘿伙计们,我正在尝试制作一款匹配纸牌游戏。 如果两张卡片匹配,用户获得一个点并且卡片保持可见,否则翻转它们(或 setText("")) 我做了关于 Swing sleep 的研究,但我不确定如何在我的代码中实现它。我已经尝试了一切,但无法让它发挥作用。我在 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 的状态...

problem is I am inside the loop and have only access to row and col as it is clicked

然后创建一个 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 = 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();

关于java - 如何让按钮setText hibernate ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29383046/

相关文章:

java - 在java中访问其类之外的私有(private)变量

java - JRemove JButton 中的默认选择

java - 有没有办法在函数式接口(interface)中重载抽象方法?

Java Swing布局问题

java - 打开文档时将图像的 url 转换为实际图像

java - 为什么我在检索 sling 的页面属性时收到 "check if the declared type is right and if the method exists"?

java - 我的一个面板没有显示

java - JButton 上的 Font Awesome

java - 实现 JTextField 的 Action 监听器

Java:重置按钮不起作用