如何撤消JButton执行的操作

Joh*_*ohn 2 java swing jframe jbutton

我目前正在研究一种类似拼字游戏的基本实现,即在Swing上随机字母形成单词,我需要帮助它的一个方面.为了简单地解释一下这个视图,我在中心面板上创建了一个6X6的JButtons网格(我已经实现了它作为图块),在顶部面板上有两个Jbuttons(Submit和Done).我的ActionPerformed方法的代码如下所示.请注意,我有一个名为Tile的单独类,它提供了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
            }
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

如果用户选择了错误的单词并单击"提交"按钮,我想撤消所有选定的图块,这些图块应该恢复正常.或者,我可以添加一个用户可以手动选择的撤消按钮.我起初想要实现一种方法来重新排列磁贴,但这对我来说很难,我决定撤消单击的按钮.

有人可以帮我这个吗?我会很感激的.

Jam*_*zba 5

使用a跟踪已选择的切片 Stack.

Stack<Tile> history = new Stack<Tile>();
Run Code Online (Sandbox Code Playgroud)

在你的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
 }
Run Code Online (Sandbox Code Playgroud)

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

if(!choice.equals("Done") && !choice.equals("Submit"))
Run Code Online (Sandbox Code Playgroud)

这是为每个不等于"完成"或"提交"的动作命令执行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"))
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)