【发布时间】: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
}
}
}
}
如果用户选择了错误的单词并单击了提交按钮,我想撤消所有选定的图块,这应该会恢复正常。或者,我可以添加一个用户可以手动选择的撤消按钮。起初我想实现一种方法来洗牌,但这对我来说很难,我决定宁愿撤消点击的按钮。
有人可以帮我解决这个问题吗?我会很感激的。
【问题讨论】: