Java 如何获得按钮数组/使其更高效地运行

Java 如何获得按钮数组/使其更高效地运行,java,arrays,jbutton,delay,Java,Arrays,Jbutton,Delay,我正在制作一个复杂的tic tac toe程序,它具有可变的网格大小和玩家数量。但我的一个朋友评论说,他们在64乘64的空间中移动后,反应有点慢。我查看了一下,发现了问题,我的程序检查了网格中的每个按钮,看它是否被点击,然后检查每个玩家,看是谁做出了这个动作。一旦两者都找到了,它就会继续生活:D。但在使用更大的网格时,这可能需要一些时间。所以我试着用一些“中断”来修复它,但它们并没有帮助我更快地找到它,只是停止了寻找 public void actionPerformed(ActionEvent

我正在制作一个复杂的tic tac toe程序,它具有可变的网格大小和玩家数量。但我的一个朋友评论说,他们在64乘64的空间中移动后,反应有点慢。我查看了一下,发现了问题,我的程序检查了网格中的每个按钮,看它是否被点击,然后检查每个玩家,看是谁做出了这个动作。一旦两者都找到了,它就会继续生活:D。但在使用更大的网格时,这可能需要一些时间。所以我试着用一些“中断”来修复它,但它们并没有帮助我更快地找到它,只是停止了寻找

public void actionPerformed(ActionEvent gridButtonClicked) {
        for (int i = 0; i < gridButton.length; i++) {
            if (gridButtonClicked.getSource() == gridButton[i]) {
                for(int a = 0;a < amountOfPlayers;a++){
                    if(turn == a) {
                        gridButtonOwner[i] = a + 1;
                        gridButton[i].setBackground(playerColors[a]);
                        gridButton[i].setEnabled(false);
                        System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
                        break;
                    }
                }
                break;
            }
        }
    }
public void actionPerformed(ActionEvent gridbutton点击){
对于(int i=0;i
我想知道的是,如果我能得到点击按钮的数组号。就像如果gridButtonClicked=gridButton[1],它将返回数字1,或者如果它等于gridButton[2],它将返回2,以此类推

  • GridButtonNowner是一个int数组
  • gridButton是一个jbutton数组

我必须说我不理解循环的必要性。在第一个for循环中,您将获得网格按钮-但您知道这是什么,因为它是事件源。。。您只需将
GridButton
的映射存储到Integer上即可获得数组中的位置。为什么要找到它?在第二个循环中,循环直到
a==turn
。。。这意味着您已经知道
a
是什么,因为它
==turn
。您应该能够完全删除循环:

// earlier on: myMap = new HashMap<GridButton, Integer>();
public void actionPerformed(ActionEvent gridButtonClicked) {
    GridButton gridButton = gridButtonClicked.getSource();
    int i = myMap.get(gridButton);
    gridButtonOwner[i] = turn + 1;
    gridButton.setBackground(playerColors[turn]);
    gridButton.setEnabled(false);
    System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
}
//前面的:myMap=newhashmap();
已执行公共作废操作(ActionEvent gridButtonClicked){
GridButton=gridButtonClicked.getSource();
inti=myMap.get(gridButton);
GridButtonNowner[i]=旋转+1;
gridButton.setBackground(播放颜色[旋转]);
gridButton.setEnabled(假);
System.out.println(“Grid”+i+:“+gridButtonOwner[i]);
}

我必须说我不明白循环的必要性。在第一个for循环中,您将获得网格按钮-但您知道这是什么,因为它是事件源。。。您只需将
GridButton
的映射存储到Integer上即可获得数组中的位置。为什么要找到它?在第二个循环中,循环直到
a==turn
。。。这意味着您已经知道
a
是什么,因为它
==turn
。您应该能够完全删除循环:

// earlier on: myMap = new HashMap<GridButton, Integer>();
public void actionPerformed(ActionEvent gridButtonClicked) {
    GridButton gridButton = gridButtonClicked.getSource();
    int i = myMap.get(gridButton);
    gridButtonOwner[i] = turn + 1;
    gridButton.setBackground(playerColors[turn]);
    gridButton.setEnabled(false);
    System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
}
//前面的:myMap=newhashmap();
已执行公共作废操作(ActionEvent gridButtonClicked){
GridButton=gridButtonClicked.getSource();
inti=myMap.get(gridButton);
GridButtonNowner[i]=旋转+1;
gridButton.setBackground(播放颜色[旋转]);
gridButton.setEnabled(假);
System.out.println(“Grid”+i+:“+gridButtonOwner[i]);
}

如果使用HashMap,可以在单个查找中找到索引“i”(无循环)。 您必须使用正确的类型(例如JButton,我不知道您使用的是哪种类型来代替GridButton)

要查找gridButton的“i”:

Integer index = buttonIndices(gridButton);
if (index == null) {
  // error, not found
  error handling stuff...
}

i = index; // converts Integer to int type of 'i'

这将使您朝着正确的方向前进。

如果使用HashMap,您可以在单个查找中找到索引“i”(无循环)。 您必须使用正确的类型(例如JButton,我不知道您使用的是哪种类型来代替GridButton)

要查找gridButton的“i”:

Integer index = buttonIndices(gridButton);
if (index == null) {
  // error, not found
  error handling stuff...
}

i = index; // converts Integer to int type of 'i'
这会让你朝着正确的方向前进