Java 如何撤消JButton执行的操作
我目前正在开发一个类似于拼字游戏的基本实现,该游戏通过Swing上的随机字母组成单词,我需要一个方面的帮助。简而言之,为了解释这个视图,我在中心面板创建了一个6X6网格的JButtons(我已经实现为tiles),在顶部面板创建了两个JButtons(Submit和Done)。下面给出了ActionPerformed方法的代码。请注意,我有一个单独的类,名为Tile,它提供JButton的图形表示,并具有与JButton相同的方法Java 如何撤消JButton执行的操作,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我目前正在开发一个类似于拼字游戏的基本实现,该游戏通过Swing上的随机字母组成单词,我需要一个方面的帮助。简而言之,为了解释这个视图,我在中心面板创建了一个6X6网格的JButtons(我已经实现为tiles),在顶部面板创建了两个JButtons(Submit和Done)。下面给出了ActionPerformed方法的代码。请注意,我有一个单独的类,名为Tile,它提供JButton的图形表示,并具有与JButton相同的方法 public void actionPerformed(Acti
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
}
}
}
}
public void actionPerformed(ActionEvent e)
{
字符串选择=e.getActionCommand();
如果(!choice.equals(“完成”)和(&!choice.equals(“提交”))
{
对于(int j=0;j跟踪使用堆栈选择的分幅
Stack<Tile> history = new Stack<Tile>();
另外,我建议您更改这一行代码:
if(!choice.equals("Done") && !choice.equals("Submit"))
这是为每个不等于“Done”或“Submit”的操作命令执行if-then代码块。现在您将有一个Undo命令,它将执行它,这不是您想要的
编辑:
根据要求提供更完整的代码示例:
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"))
{
//...
}
}
Stack history=新堆栈();
已执行的公共无效操作(操作事件e)
{
字符串选择=e.getActionCommand();
if(choice.equals(“单击”))
{
for(int j=0;j感谢您的回答。由于我对单击按钮的for循环超出了此if语句的范围,我不明白如何启用每个单击的按钮。有什么想法吗?我不理解您的问题,for循环:for(int j=0;j请给我一分钟,我将包括您提供给我的更完整的代码示例。请参阅我的编辑。请注意,我添加了一个新的操作命令“单击”。您需要为平铺单击指定该操作。
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"))
{
//...
}
}