gpt4 book ai didi

java - 如何从两个 JFrame 上的 2 个按钮数组中判断按下了哪个按钮

转载 作者:行者123 更新时间:2023-12-01 21:43:04 24 4
gpt4 key购买 nike

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

最佳答案

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

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

并调用 getActionCommand 来检索此字符串。或者,安装 map

Map<JButton,ButtonData> but2dat

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

关于java - 如何从两个 JFrame 上的 2 个按钮数组中判断按下了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com