java - 比较不同 JButton 的文本

标签 java swing loops graphics jbutton

我目前正在编写一个游戏,其中一部分是将不同的方 block 放入棋盘中。我计划通过使用不同的按钮来模拟这一点,这些按钮将用于表示具有相应坐标的图 block 。例如,一个按钮会显示“A1”、“A2”等。我想要完成的是让用户单击“A1”图 block ,然后板上代表“A1”的按钮将发生变化颜色,有没有办法通过板上的按钮并将其文本与用户的选择进行比较?以下是我用来创建板的内容:

    JButton[][] buttons = new JButton[9][12];
    JPanel panel = new JPanel(new GridLayout(9,12,5,5));
    panel.setBounds(10, 11, 800, 600);
    frame.getContentPane().add(panel);


    //board
     for (int r = 0; r < 9; r++)
        {
            for (int c = 0; c < 12; c++)
            {
                buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
                buttons[r][c].setBackground(Color.WHITE);
                panel.add(buttons[r][c]);
            }

        }

这是我在其中一个磁贴的代码上写的

JButton tile1 = new JButton ("A1");
        tile1.setBounds(60,725,60,60);
        frame.getContentPane().add(tile1);

        tile1.addActionListener(new ActionListener() 
         { 
                     public void actionPerformed(ActionEvent e) 
            { 
                String buttonText = tile1.getText();

                // iterating through all buttons:

                for(int i=0;i<buttons.length;i++){
                    for(int j=0;j<buttons[0].length;j++)
                    {
                        JButton b = buttons[i][j];
                        String bText = b.getText(); 

                       if(buttonText.equals(bText))
                        {
                            [i][j].setBackground(Color.BLACK);
                        }
                    }
                }
             } 
          } );

但是,它给我一个错误,说在“{”之后有一个预期的 Action

最佳答案

您可以为您在循环中创建的每个 JButton 添加一个 Action 监听器,如下所示:

buttons[r][c].addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent e) { 
    // your code here
  } 
} );

将监听器放在您的代码中可能看起来像

JButton[][] buttons = new JButton[9][12];
    JPanel panel = new JPanel(new GridLayout(9,12,5,5));
    panel.setBounds(10, 11, 800, 600);
    frame.getContentPane().add(panel);


    //board
     for (int r = 0; r < 9; r++)
        {
            for (int c = 0; c < 12; c++)
            {
                buttons[r][c] = new JButton("" + (c + 1) + numberList[r]);
                buttons[r][c].setBackground(Color.WHITE);
                buttons[r][c].addActionListener(new ActionListener() { 
                   public void actionPerformed(ActionEvent e) { 
                      JButton button = (JButton) e.getSource();
                      String buttonText = button.getText();
                      // now iterate over all the jbuttons you have
                      for(int i=0;i<buttons.length;i++){
                          for(int j=0;j<buttons[0].length;j++){
                              JButton b = buttons[i][j];
                              String bText = b.getText();
                              if(buttonText.equals(bText)){
                                  // you found a match here
                                  // and you have the positions i, j
                                  // 
                              }
                          }
                      }
                   } 
                } );
                panel.add(buttons[r][c]);
            }

        }

并且您可以将要更改的颜色存储在全局静态数组中,并在您的 Action 监听器中使用该数组。

有关向 JButton 添加监听器的信息,您可以引用此线程 How do you add an ActionListener onto a JButton in Java

希望这对您有所帮助!

关于java - 比较不同 JButton 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44148901/

相关文章:

java - 如何在 Swing 延迟几秒后处理帧?

javascript - 延迟多次调用函数的最快方法

c# - 如何在 C# 插入查询中使用循环

java - 学生在这里,问题 While 循环不起作用

java - 在卡片布局中切换卡片时 JMenuBar 不显示

java - 如何在 Java 中为分隔符指定多种模式

java - "for"函数对 ArrayList 的操作和异常 java.lang.ArrayIndexOutOfBoundsException

java - Go 中的 Java Arrays.copyOfRange 等价于什么?

java - 如何访问我的代码中的数据? -变量可能尚未初始化

java - 这两种创建对象的方式有什么不同