如何撤消 JButton 执行的操作

How to undo actions performed by a JButton

我目前正在研究一个类似拼字游戏的基本实现,该游戏是在 Swing 上从随机字母组成单词,我需要它的一个方面的帮助。为了简要解释该视图,我在中央面板创建了一个 6X6 的 JButton 网格(我已将其实现为图块),在顶部面板有两个 Jbutton(提交和完成)。下面给出了我的 ActionPerformed 方法代码。请注意,我有一个名为 Tile 的单独 class,它提供了 JButton 的图形表示,并且具有与 JButton 相同的方法。

public void actionPerformed(ActionEvent e)
{
    String choice = e.getActionCommand();

    if(!choice.equals("Done") && !choice.equals("Submit"))
    {
        for(int j=0; j<6; j++)
        {
            for(int k=0; k<6; k++)
            {

            if(tile[j][k]==e.getSource())
            { 
                current+=(tile[j][k].getTile().letter());  //gets each letter of the clicked Tiles and adds it to a String variable 'current'
                score+=(tile[j][k].getTile().value());    //gets the value of the tiles to calculate the score

                tile[j][k].setForeground(Color.blue);
                tile[j][k].removeActionListener(this);
                tile[j][k].setEnabled(false); //the tile can only be clicked once

                //rest of the code to set rules for adjacent tiles etc
            }
        }
    }
}  

如果用户 select 说错了词并单击了提交按钮,我想撤消所有 selected 的图块,这应该会恢复正常。或者,我可以添加一个撤消按钮,用户可以手动 select。起初我想实现一种方法来洗牌,但这对我来说很难,所以我决定撤消点击的按钮。

有人可以帮我解决这个问题吗?我会感激的。

跟踪已使用 Stack 选择的图块。

Stack<Tile> history = new Stack<Tile>();

在您的 actionPerformed 方法中:

if(tile[j][k]==e.getSource())
 {
     ...
     history.push(tile[j][k]);
     ...
 }

 if(choice.equals("Undo"))
 {
     Tile previous = history.pop(); //be sure to handle EmptyStackException 
     //TODO undo the actions of the Tile: subtract score, remote letter, enable the button
 }

另外,我建议你更改这行代码:

if(!choice.equals("Done") && !choice.equals("Submit"))

这是为每个不等于 "Done" 或 "Submit" 的动作命令执行 if-then 代码块。现在你会有一个撤消命令,它会执行它,这不是你想要的。

编辑:

更完整的代码示例,应要求提供:

Stack<Tile> history = new Stack<Tile>();

public void actionPerformed(ActionEvent e)
{
    String choice = e.getActionCommand();

    if(choice.equals("Click"))
    {
        for(int j=0; j<6; j++)
        {
            for(int k=0; k<6; k++)
            {
                if(tile[j][k]==e.getSource())
                { 
                    current+=(tile[j][k].getTile().letter());  //gets each letter of the clicked Tiles and adds it to a String variable 'current'
                    score+=(tile[j][k].getTile().value());    //gets the value of the tiles to calculate the score

                    tile[j][k].setForeground(Color.blue);
                    tile[j][k].removeActionListener(this);
                    tile[j][k].setEnabled(false); //the tile can only be clicked once

                    history.push(tile[j][k]);

                    // rest of the code to set rules for adjacent tiles etc
                }
            }
        }
    } 
    else if(choice.equals("Undo"))
    {
        Tile previous = history.pop(); //be sure to handle EmptyStackException 
        //TODO undo the actions of the Tile: subtract score, remote letter, enable the button
    }
    else if(choice.equals("Submit"))
    {
        //...
    }
    else if(choice.equals("Done"))
    {
        //...
    }
}