2D JButton数组,要为游戏添加动作侦听器

问题描述 投票:0回答:2

我想制作TIC TAC TOE游戏,但有问题。我制作了一个2D数组,但我不知道如何寻址它们才能使ActionListener工作。

这是我的代码:

public class GUI {
    public static JButton[][] buttonsall = new JButton[3][3];
    public static JFrame frame = new JFrame("TIC TAC TOE");
    public static void draw() {
        // frame
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBackground(Color.white);
        frame.setBounds(500, 500, 600,600);
        // actionListener
        ActionListener listener = new ActionListener() {
             @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() instanceof JButton) {
                        //dont know further here
                    }
                }
            };

        //buttons buttonsall[x][y]
        for (int y = 0; y<3;y++) {
            for (int x = 0; x<3;x++) {
                buttonsall[x][y] = new JButton();
                buttonsall[x][y].setVisible(true);
                buttonsall[x][y].setSize(80, 80);
                buttonsall[x][y].addActionListener(listener);
                System.out.println(buttonsall[x][y] +" "+x +" "+y);
                frame.add(buttonsall[x][y]);
                buttonsall[x][y].setBackground(Color.white);    
            }   
        }
        frame.setLayout(new GridLayout(3,3));
    }

java arrays jbutton
2个回答
0
投票

您可以在创建每个JButton时为每个JButton设置setName。我将更新您的代码,如下所示:


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI {
    public static JButton[][] buttonsall = new JButton[3][3];
    public static JFrame frame = new JFrame("TIC TAC TOE");

    public static void draw() {
        // frame
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBackground(Color.white);
        frame.setBounds(500, 500, 600, 600);
        // actionListener
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() instanceof JButton) {
                    //dont know further here
                    JButton b = (JButton)e.getSource();

                    System.out.println("Button is:" + b.getName());
                }
            }
        };

        //buttons buttonsall[x][y]
        for (int y = 0; y < 3; y++) {
            for (int x = 0; x < 3; x++) {
                buttonsall[x][y] = new JButton();
                buttonsall[x][y].setVisible(true);
                buttonsall[x][y].setName("Button_" + x + "_" + y);
                buttonsall[x][y].setSize(80, 80);
                buttonsall[x][y].addActionListener(listener);
                System.out.println(buttonsall[x][y] + " " + x + " " + y);
                frame.add(buttonsall[x][y]);
                buttonsall[x][y].setBackground(Color.white);
            }
        }
        frame.setLayout(new GridLayout(3, 3));
    }

    public static void main(String[] args) {

        GUI.draw();

    }
}


0
投票

您的问题与this question非常相似。

基本上,您想使用ActionEvent.getSource()并将其转换为JButton。然后,您可以对JButton进行任何操作(更改背景颜色,更改文本等)。

编辑:我没有意识到您想要获得JButton的xy

public class TicTacToe {
    public static TicTacToeButton[][] buttonsall = new TicTacToeButton[3][3];
    public static JFrame frame = new JFrame("TIC TAC TOE");
    public static void draw() {
        // frame
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBackground(Color.white);
        frame.setBounds(500, 500, 600,600);
        // actionListener
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof TicTacToeButton) {
                    TicTacToeButton btn = (TicTacToeButton) source;
                    int btnBoardX = btn.getBoardX();
                    int btnBoardY = btn.getBoardY();
                    // Go ahead and do what you like
                }
            }
        };

        //buttons buttonsall[x][y]
        for (int y = 0; y<3;y++) {
            for (int x = 0; x<3;x++) {
                buttonsall[x][y] = new TicTacToeButton(x, y);
                buttonsall[x][y].setVisible(true);
                buttonsall[x][y].setSize(80, 80);
                buttonsall[x][y].addActionListener(listener);
                System.out.println(buttonsall[x][y] +" "+x +" "+y);
                frame.add(buttonsall[x][y]);
                buttonsall[x][y].setBackground(Color.white);
            }
        }
        frame.setLayout(new GridLayout(3,3));
    }

    private static class TicTacToeButton extends JButton {
        private int boardX;
        private int boardY;

        public TicTacToeButton(int boardX, int boardY) {
            super();
            this.boardX = boardX;
            this.boardY = boardY;
        }

        public int getBoardX() {
            return this.boardX;
        }

        public int getBoardY() {
            return this.boardY;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.