【问题标题】:How to undo actions performed by a JButton如何撤消 JButton 执行的操作
【发布时间】:2015-12-07 04:06:40
【问题描述】:

我目前正在开发一个类似拼字游戏的游戏的基本实现,该游戏在 Swing 上用随机字母组成单词,我需要一些方面的帮助。简而言之,我在中心面板创建了一个 6X6 的 JButton 网格(我已将其实现为图块),顶部面板有两个 Jbutton(提交和完成)。我的 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
            }
        }
    }
}  

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

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

【问题讨论】:

    标签: java swing jframe jbutton


    【解决方案1】:

    使用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"))
    

    这是为每个不等于“完成”或“提交”的操作命令执行 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"))
        {
            //...
        }
    }
    

    【讨论】:

    • 您好,感谢您的回答。由于单击的按钮的 for 循环在此 if 语句之外,因此我不明白如何启用每个单击的按钮。有什么想法吗?
    • 我不明白你的问题
    • 在 for 循环中:for(int j=0; j&lt;6; j++) { for(int k=0; k&lt;6; k++) { if(tile[j][k]==e.getSource()) { 如何在您提供的 If 语句之外使用它来启用磁贴
    • 给我一分钟,我将根据您给我的内容提供更完整的代码示例
    • 请看我的编辑。请注意,我添加了一个新的操作命令“单击”。您需要为 Tile 点击指定该操作。
    猜你喜欢
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多