且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

扫雷,我如何处理细胞

更新时间:2023-11-13 15:34:28

只需按住一个二维数组按钮,

Just hold a two dimensional array of buttons,

JButton[][] myButtons = new JButton[10][10];

你用来绘制它们,它们都用它们的值调用相同的方法

which you use to draw them and they all call the same method with their value

 for (int x=0; x<10; x++){
   for (int y=0; y<10; y++){
     myButtons[x][y] = new JButton("0");
     //add to page, or do it elsewhere
     myButtons[x][y].addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
           selectionButtonPressed(x,y);
        }
     });
   }
 }

然后调用您的方法 selectionButtonPressed() 将告诉您按下了哪个按钮,您可以采取行动,甚至可以使用 myButtons[x][y] 对按钮进行更改.whatever()

Then a call to yout method selectionButtonPressed() will tell you what button has been pressed, you can take action and even make changes to the button with myButtons[x][y].whatever()