【问题标题】:2D JButton array, want to add actionlisteners to them for a game2D JButton 数组,想要为游戏添加动作监听器
【发布时间】:2020-02-21 20:04:38
【问题描述】:

我想制作一个井字游戏,但我遇到了问题。 我制作了一个二维数组,但我不知道如何解决它们以使 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 swing jbutton


    【解决方案1】:

    您可以在创建每个 JButton 时为其设置名称。我将您的代码更新如下:

    
    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();
    
        }
    }
    
    

    【讨论】:

      【解决方案2】:

      您的问题与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;
              }
          }
      }
      

      【讨论】:

      • 现在的问题是我需要确切知道按下了哪个按钮
      • @bgt0123 我假设您想在板上获得JButtonxy。我更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      相关资源
      最近更新 更多