java - 如何在 Action 监听器中获取多个JButton的来源?

标签 java jbutton actionlistener

我正在使用图片和 JButton 数组编写内存匹配游戏,但是当我尝试比较单击的两个按钮时遇到了问题。如何存储/获取第二个按钮的索引?按钮数组中的所有按钮都链接到同一个 actionListener,但据我所知, e.getSource() 只会单击第一个按钮。我真的很感激一些帮助。 (我不想粘贴整个代码,因为代码太多,所以我只是放入我认为相关的部分):

   public DisplayMM(ActionListener e)
   {
    setLayout(new GridLayout(6, 8, 5, 5)); 
    JButton[] cards = new JButton[48]; //JButton board

    for(int x = 0; x < 48; x++) //initial setup of board
    {
     cards[x] = new JButton();
     cards[x].addActionListener(e);
     cards[x].setHorizontalAlignment(SwingConstants.CENTER);
     cards[x].setPreferredSize(new Dimension(75, 95));
     }

   private class e implements ActionListener
   {
    public void actionPerformed(ActionEvent e)
     {
     for(int i = 0; i < 48; i++)
      {
       if((e.getSource())==(cards[i]))//1st button that was clicked
        {
          cards[i].setIcon(new ImageIcon(this.getClass().getResource(country[i])));
          currentIndex = i;
        }
      }
      //cards[i].compareIcons(currentIndex, secondIndex);

     }
 }

此外,在我的 Panel 类中,我尝试执行类似的操作,但最终将其移至 Display 类,因为 Panel 无法访问按钮数组。

  //Panel

     public void actionPerformed(ActionEvent e)
     {
     /*every 2 button clicks it does something and decreases num of tries*/
        noMatchTimer = new Timer(1000, this);
        noMatchTimer.setRepeats(false);
        JButton source = (JButton)e.getSource();
         guess1 = source.getText(); //first button clicked
        numGuess++; //keeps track of number of buttons clicked
        JButton source2 = (JButton)e.getSource();
        guess2 = source2.getText();
        numGuess++; 
        if(numGuess == 1)
            display.faceUp(cards, array, Integer.parseInt(e.getSource()));
        else
            display.compareIcons(guess1, guess2);

         if(tries != 12 && count == 24)
          {
           displayWinner();
          }
     }

最佳答案

您可以为 ActionListener 类提供私有(private)字段,即使它是匿名内部类,并且这些字段之一可以是对最后按下的按钮的引用。按下第二个按钮后将其设置为 null,您将始终知道按下的按钮是针对第一个按钮还是第二个按钮。

例如,

class ButtonListener implements ActionListener {
    private JButton lastButtonPressed = null;

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        if (lastButtonPressed == null) {
            // then this is the first button
            lastButtonPressed = source;
        } else {
            // this is the 2nd button
            if (source == lastButtonPressed) {
                // the dufus is pushing the same button -- do nothing
                return;
            } else {
                // compare source and lastButtonPressed to see if same images (icons?)
                // if not the same, use a Timer to hold both open for a short period of time
                // then close both
                lastButtonPressed = null;
            }
        }
    }
}

关于java - 如何在 Action 监听器中获取多个JButton的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450526/

相关文章:

java - 你如何在它的 Action 监听器中引用一个按钮?

java - 如何使用自定义 HttpAdaptor 将电子锁跟踪器 (JT701) 与 Eclipse Hono 集成

java - 如何使用 Java 在 Selenium WebDriver 的隐藏字段中键入一些文本

java - 在 for 循环中创建 JButton 后将其删除

java - 改变JButton的背景颜色

java - Java 中带有键绑定(bind)的 keyReleased() 方法?

java - 运行 gradle 构建时出错

java - 如何在我的测试代码中直接使用 HibernateTemplate

java - 如何使用按钮切换到特定的JPanel?

java - 从 SPinnerDateModel 的 Jspinner 获取日期