2014-02-17 56 views
0

所以,我正在使用GridLayouttictactoetile的一个项目,这是一个扩展JButton类的项目。 对于我试图做的第一件事,我想知道点击了哪个按钮,然后将该按钮的文本设置为等于x或o,具体取决于它是哪一个。我有这样的逻辑,我只是​​不知道如何获得点击的按钮的行和列。 对不起,如果措辞不好。处理tictactoe检测按钮

+0

你能将坐标存储为'TicTacToeTile'的字段吗? –

+0

假设你使用的是ActionListener,你就可以访问生成事件的'source' ... – MadProgrammer

+0

我假设你的意思是evt.getsource(),这是我想用的,但我可以'弄清楚如何应用它。 – user3316794

回答

0
public class tictactoetile extends JButton implements ActionListener { 

    private int cory; 
    private int corx; 
    //Create your own GameObj class with the necessary members 
    //Some examples for members below... 
    private GameObj game; 

    public tictactoetile(int x,int y,GameObj gam) { 
     cory = y; 
     corx = x; 
     game = gam; 
     super(); 
    } 

    public void actionPerformed(ActionEvent e) { 
     this.setText(game.getTurnMarker()); //returns either x or o 
     game.updateGameState(game.getTurn(),cory,corx); 
     game.nextTurn(); 
    } 
}