【问题标题】:Why does my firstCardClicked.setDisabledIcon(img) work, but my secondCardClicked.setDisabledIcon(img) not work?为什么我的 firstCardClicked.setDisabledIcon(img) 有效,但我的 secondCardClicked.setDisabledIcon(img) 无效?
【发布时间】:2017-05-04 03:54:18
【问题描述】:

我正在构建一个 Java Swing 记忆游戏。这个想法是点击第一张卡片,它将图像从baseImage更改为另一个图像。当第二张卡片被点击时,它应该做同样的事情,等待几秒钟,然后如果它们不匹配则将所有内容重置回基础,如果匹配则将它们翻转过来。

现在,如果它们不匹配,第二张卡片永远不会改变图像。下面是相关代码。应该注意的是,如果它们确实匹配,则图像显示并且世界上一切都是正确和美好的。

public void cardClickHandler(ActionEvent ae) {
    JButton card = new JButton();
    card = (JButton) ae.getSource();

    // disable the card that was clicked
    card.setEnabled(false);

    // behavior for first card clicked 
    if (clickCounter == 0) {
        firstCardClicked = card;
        img = new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg");
        firstCardClicked.setDisabledIcon(img);
        System.out.println("Button " + firstCardClicked.getName() + " clicked!");

        clickCounter++;
    }
    // behavior for second card clicked
    else {
        secondCardClicked = card;
        img = new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg");
        secondCardClicked.setDisabledIcon(img);
        System.out.println("Button " + secondCardClicked.getName() + " clicked!");

        clickCounter--;
    }

    // behavior if two cards have been clicked and they match
    if (firstCardClicked.getName().equals(secondCardClicked.getName()) && clickCounter == 0) {
        // player turn control and scoring
        if (p1Turn) {
            p1NumScore++;
            p1Score.setText(Integer.toString(p1NumScore));
            scorePanel.revalidate();
            System.out.println("Good job Mike, got a pair!");
            p1Turn = !p1Turn;
        }
        else {
            p2NumScore++;
            p2Score.setText(Integer.toString(p2NumScore));
            scorePanel.revalidate();
            System.out.println("Good job Peanut, got a pair!");
            p1Turn = !p1Turn;
        }
    }
    // behavior if two cards have been clicked and they do not match
    else if (!(firstCardClicked.getName().equals(secondCardClicked.getName())) && clickCounter == 0) {
        // keep cards flipped for a few seconds
        try {
            System.out.println("Before Sleep");         // testing
            Thread.sleep(2000);
            System.out.println("After Sleep");          // testing
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // enable the cards and reset images
        firstCardClicked.setEnabled(true);
        firstCardClicked.setIcon(baseImage);
        secondCardClicked.setEnabled(true);
        secondCardClicked.setIcon(baseImage);

        // change turns
        p1Turn = !p1Turn;
        System.out.println("Keep Playing");
    }
}

【问题讨论】:

  • 不要在事件处理中使用Thread.sleep(int)。它会阻止 GUI。
  • 还请提供一个简短的可运行示例 (MVCE)。
  • @SergiyMedvynskyy 作为初学者,我试图远离Thread.sleep(int),因为它与 Swing 存在问题。不幸的是,我无法找到正确实现 Swing Timer 的方法,而且效果很好。明天我将尝试将其分解为 MVCE。欣赏一切。

标签: java image swing


【解决方案1】:

看起来好像有几个问题同时工作。 Thread.sleep().setEnabled(true) 命令一起造成了严重破坏。对象的状态存在问题,因此我需要在代码末尾清除它们。 .setDisabledIcon() 方法存在一些已知问题,这里的许多帖子都说需要 .setIcon() 才能在它之前。这是与正在使用的javax.swing.Timer 一起使用的固定代码。

public void cardClickHandler(ActionEvent ae)
{
    // method variables
    JButton card = (JButton) ae.getSource();
    boolean clearState = false;


    // behavior for first card clicked 
    if (isFirstCard)
    {
        firstCardClicked = card;

        // a known issue with "setDisabledIcon()" required the "setIcon()" method
        firstCardClicked.setIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg"));
        firstCardClicked.setDisabledIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg"));

        // disable the flipped card
        firstCardClicked.setEnabled(false);

        // indicate the next card clicked is the second card
        isFirstCard = false;
    }
    // behavior for second card clicked
    else
    {
        secondCardClicked = card;

        // a known issue with "setDisabledIcon()" required the "setIcon()" method
        secondCardClicked.setIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg"));
        secondCardClicked.setDisabledIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg"));

        // disable the flipped card
        secondCardClicked.setEnabled(false);

        // indicate the next card clicked is the first card again
        isFirstCard = true;

        // indicate to the system both cards have been clicked and can clear objects
        clearState = true;
    }

    // behavior if two cards have been clicked and they match
    if (isFirstCard && firstCardClicked.getName().equals(secondCardClicked.getName()))
    {
        // player turn control and scoring
        if (p1Turn)
        {
            p1NumScore++;
            p1Score.setText(Integer.toString(p1NumScore));
            scorePanel.revalidate();
            p1Turn = !p1Turn;
        }
        else
        {
            p2NumScore++;
            p2Score.setText(Integer.toString(p2NumScore));
            scorePanel.revalidate();
            p1Turn = !p1Turn;
        }
    }
    // behavior if two cards have been clicked and they do not match
    else if (isFirstCard && !(firstCardClicked.getName().equals(secondCardClicked.getName())) )
    {
        // enable the cards and reset images
        firstCardClicked.setIcon(BASE_IMAGE);
        secondCardClicked.setIcon(BASE_IMAGE);

        // change turns
        p1Turn = !p1Turn;
    }
    if (clearState)
    {
        javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent ae) {

                        firstCardClicked.setEnabled(true);
                        secondCardClicked.setEnabled(true);
                        isFirstCard = true;
                        firstCardClicked = null;
                        secondCardClicked = null;

            }
        });

    // Normally the timer's actionPerformed method executes repeatedly. Tell it only to execute once.
    timer.setRepeats(false);

    // Start the timer.
    timer.start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-11
    • 2019-11-07
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多