如何从两个JFrame上的2个数组按钮中分辨出正在按下的按钮

时间:2016-03-26 17:04:49

标签: java swing jframe jbutton

我有两个Jframe中显示的2个按钮阵列,我需要告诉哪一个被按下,我当前的解决方案只检测到按下第一个窗口的按钮,所以当你点击第一个窗口上的按钮时在程序重新启动之前,将无法单击第二个窗口上的按钮。

我的代码是

 private void populateArray()
{
    for(int wy = 0;wy<8;wy++)
    {
        for(int wx =0; wx<8;wx++)
        {
            WhiteButton[wx][wy] = new ReversiButton();
            WhiteButton[wx][wy].addActionListener(this);
            if((wx == 3)&&( wy ==3))
                WhiteButton[wx][wy].change(1);
            else if((wx == 4)&&(wy == 3))
                WhiteButton[wx][wy].change(2);
            else if((wx == 3)&&(wy == 4))
                WhiteButton[wx][wy].change(2);
            else if((wx ==4)&&(wy == 4))
                WhiteButton[wx][wy].change(1);
        }
    }
    for(int by = 0; by<8; by++)
    {
        for(int bx =0; bx<8;bx++)
        {
            BlackButton[bx][by] = new ReversiButton();
            BlackButton[bx][by].addActionListener(this);
            if((bx == 3)&&( by ==3))
                BlackButton[bx][by].change(1);
            else if((bx == 4)&&(by == 3))
                BlackButton[bx][by].change(2);
            else if((bx == 3)&&(by == 4))
                BlackButton[bx][by].change(2);
            else if((bx ==4)&&(by == 4))
                BlackButton[bx][by].change(1);
        }
    }
}
private void initGUI()
{
    WhiteFrame.setTitle("Reversi White Player");
    BlackFrame.setTitle("Reversi Black Player");
    WhiteFrame.setLayout(new BorderLayout());
    WhiteLabel.setText("White Player - click place to put piece");
    WhiteGrid.setLayout(new GridLayout(8,8));
    for(int wy = 0;wy<8;wy++)
    {
        for(int wx =0; wx<8;wx++)
        {
            WhiteGrid.add(WhiteButton[wx][wy]);
        }
    }
    WhiteButtons.setText("Greedy AI(play white)");
    WhiteFrame.add(BorderLayout.NORTH,WhiteLabel);
    WhiteFrame.add(BorderLayout.CENTER,WhiteGrid);
    WhiteFrame.add(BorderLayout.SOUTH,WhiteButtons);
    WhiteFrame.pack();
    WhiteFrame.setVisible(true);
    BlackFrame.setLayout(new BorderLayout());
    BlackLabel.setText("Black player - not your turn");
    BlackGrid.setLayout(new GridLayout(8,8));
    BlackButton = Reverse.rotatearray(BlackButton);
    for(int by = 0; by<8; by++)
    {
        for(int bx =0; bx<8;bx++)
        {
            BlackGrid.add(BlackButton[bx][by]);
        }
    }
    BlackButtons.setText("Greedy AI(play black)");
    BlackFrame.add(BorderLayout.NORTH, BlackLabel);
    BlackFrame.add(BorderLayout.CENTER, BlackGrid);
    BlackFrame.add(BorderLayout.SOUTH,BlackButtons);
    BlackFrame.pack();
    BlackFrame.setLocation(WhiteFrame.getX() + WhiteFrame.getWidth() + 10, WhiteFrame.getY());
    BlackFrame.setVisible(true);
}
 @Override
    public void actionPerformed(ActionEvent ae)
    {
        for (int y = 0; y < GRIDSIZE; y++)
        {
            for (int x = 0; x < GRIDSIZE; x++)
            {
                if((ae.getSource()==WhiteButton[x][y]))
                {
                    if(WhiteButton[x][y].is() ==0)
                    {
                        WhiteButton[x][y].change(1);
                        Game.search(WhiteButton, x, y, 1);
                    }
                }
                if((ae.getSource() == BlackButton[x][y]))
                {
                    if(BlackButton[x][y].is() == 0)
                    {
                        BlackButton[x][y].change(2);
                        Game.search(BlackButton, x, y, 2);
                    }
                }
            }
        }
    }

如何从两个JFrame中检测按钮按下?

1 个答案:

答案 0 :(得分:3)

您可以为每个按钮定义一个动作命令:

 WhiteButton[wx][wy].setActionCommand(w + wx + "," + wy);

并调用getActionCommand来检索此String。或者,装配一个地图

Map<JButton,ButtonData> but2dat

并将坐标和当前状态存储在ButtonData类的对象中。