我目前正在研究一种类似拼字游戏的基本实现,即在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)
如果用户选择了错误的单词并单击"提交"按钮,我想撤消所有选定的图块,这些图块应该恢复正常.或者,我可以添加一个用户可以手动选择的撤消按钮.我起初想要实现一种方法来重新排列磁贴,但这对我来说很难,我决定撤消单击的按钮.
有人可以帮我这个吗?我会很感激的.