【问题标题】:How to access a JButton from a matrix [duplicate]如何从矩阵访问 JButton [重复]
【发布时间】:2017-04-24 04:39:09
【问题描述】:

我正在创建一个简单的井字游戏。我使用 [3][3] 矩阵创建了 9 个 JButton。问题是我需要检查这些按钮的状态,以便确定获胜者。

我不知道如何在没有某种标识符或索引的情况下通过 if 语句检查/访问这些按钮。我可以创建 9 个 JButton 对象并使用 if 语句检查它们,但它似乎效率不高。 我知道这不是这个游戏的最佳解决方案,我应该为游戏逻辑创建一个完整的类,但是这种方式似乎非常有效,我真的很想知道如何做到这一点。

代码如下:

import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.GridLayout;

public class AdinTicTacToe extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 500; //Width of the JFrame
    public static final int HEIGHT = 400; //Height of the JFrame

    public static void main(String[] args) {
        AdinTicTacToe gui = new AdinTicTacToe(3, 3);
        gui.setVisible(true);
    }

    //Creating a matrix of buttons to make flexible layout
    JButton[][] buttons = new JButton[3][3];

    {
        for (int row = 0; row < buttons.length; row++) {
            for (int col = 0; col < buttons[0].length; col++) {
                JButton cells = new JButton();
                buttons[row][col] = cells;
                add(cells);
                cells.addActionListener(this);
            }
        }
    }

    //A constructor to set initial values
    public AdinTicTacToe(int rows, int columns) {
        super();
        setSize(WIDTH, HEIGHT);
        setTitle("Tic Tac Toe");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Setting a layout
        setLayout(new GridLayout(rows, columns));
    }

    //Handling button clicks
    boolean check; //Variable to determine the current state of a button

    @Override
    public void actionPerformed(ActionEvent e) {
        //Using getSource method to avoid multiple if statements and make it efficient
        JButton myButton = (JButton) e.getSource();
        if (!check)
            myButton.setText("X");
        ; //Set X to the clicked cell
        if (check)
            myButton.setText("O"); //Set O to the clicked cell
        check = !check; //Reverting the button state 
        myButton.setFont(new Font("Arial", Font.BOLD, 60)); //Set font of X and O
        myButton.setEnabled(false); //Disable button after it gets clicked

    }
}

【问题讨论】:

  • 你想在哪里查看他们的state
  • 我想在 actionPerform 方法中使用 if 语句来检查它,但我没有这些按钮的索引。
  • 为什么需要索引?什么不工作?
  • 我需要在有人获胜时显示消息。我需要检查是否有三个XXX在同一行、同一列或同一对角线
  • 啊。我知道了。好吧,请稍等

标签: java swing multidimensional-array


【解决方案1】:

首先:初始化之前不需要括号。

OOP-ish 方法是创建一个包含对按钮的引用和按钮索引的类:

public class ButtonIndexPair{
    private JButton button;
    private int indexRow;
    private int indexColumn;
    public ButtonIndexPair(JButton button,int indexRow, int indexColumn){
        this.button = button;
        this.indexRow = indexRow;
        this.indexColumn = indexColumn;
    }
    public int getRow(){
        return indexRow;
    }
    public int getColumn(){
        return indexColumn;
    }
    public JButton getButton(){
        return button;
    }
}

然后当你初始化按钮数组时,改为这样做:

JButton cells = new JButton();
buttons[row][col] = new ButtonIndexPair(cells,row,col);
add(cells);
cells.addActionListener(this);

并将矩阵的类型更改为ButtonIndexPair

ButtonIndexPair[][] buttons = new ButtonIndexPair[3][3];


@Override
public void actionPerformed(ActionEvent e) {
    //Using getSource method to avoid multiple if statements and make it efficient
    JButton myButton = (JButton) e.getSource();
    ButtonIndexPair theRightPair = buttons[0][0];
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons[row].length; col++) {
            if(buttons[row][col].getButton().equals(myButton)){
                theRightPair = buttons[row][col];
                break;
            }
        }
    }

    //then you can use 'theRightPair' 

    if (!check)
        myButton.setText("X");
    ; //Set X to the clicked cell
    if (check)
        myButton.setText("O"); //Set O to the clicked cell
    check = !check; //Reverting the button state 
    myButton.setFont(new Font("Arial", Font.BOLD, 60)); //Set font of X and O
    myButton.setEnabled(false); //Disable button after it gets clicked

 }

【讨论】:

  • 但是如何改变矩阵类型呢?
  • @AdinCebic 编辑的答案
  • 是的,这个接缝很好,但由于构造函数的原因,我无法创建该类的对象。我需要一个对象来调用 getRow 和 getColumn 方法以获取这些索引。我该怎么做?
  • @AdinCebic 您可以循环遍历二维数组,直到找到一个类似于单击按钮的按钮 (buttons[i][j].getButton().equals(myButton))
  • 我不是很有经验,你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 2017-08-30
  • 2018-07-14
  • 2019-02-28
  • 1970-01-01
  • 2014-12-05
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多