【问题标题】:How to tell what button is being pressed from 2 array's of buttons on two JFrames如何从两个 IFrame 上的 2 个按钮数组中判断正在按下哪个按钮
【发布时间】:2016-03-26 17:04:48
【问题描述】:

我在两个 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 的按钮按下?

【问题讨论】:

标签: java swing jframe jbutton


【解决方案1】:

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

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

并调用 getActionCommand 来检索此字符串。或者,装配一张地图

Map<JButton,ButtonData> but2dat

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2010-10-18
    • 2015-04-04
    相关资源
    最近更新 更多