【问题标题】:How to count how many JButtons have been clicked from a 10 x 10 java JButton grid如何计算从 10 x 10 java JButton 网格中单击了多少 JButton
【发布时间】:2017-08-27 13:04:03
【问题描述】:

我想计算从 10 x 10 JButton 网格中被点击的 JButton 的数量。

这就是我要说的

无论如何,我不知道如何计算点击了多少 JButton。我想过制作 100 个 JButton,但这似乎很愚蠢。

还有如何防止超过 14 个按钮被点击?

ActionListener al = new ActionListener()
{
        public void actionPerformed(ActionEvent e)
        {
             JButton button = (JButton)e.getSource();
             button.setEnabled( false );

        }
};

    for(int row = 0; row < 10; row++)
    {
       for(int col = 0; col < 10; col++)
       {
               button = new JButton();
               button.addActionListener( al );
               panel_1.add(button);
       }
    }

这是我的 forLoops,用于制作 100 个按钮并为每个按钮提供一个 actionListener,因此当单击 JButton 时,它变得不可单击。

    ActionListener al = new ActionListener()
    {
        int clicked = 0;

        public void actionPerformed(ActionEvent e)
        {
            button = (JButton)e.getSource();

             if(clicked != 14)
             {
                 clicked++;
             }
             else

                button = (JButton)e.getSource();
                button.setEnabled( false );

        }
    };

    for(int row = 0; row < 10; row++)
    {
        for(int col = 0; col < 10; col++)
        {
                button = new JButton();
                button.addActionListener( al );
                panel_1.add(button);

        }
    }

我试过设置一个柜台,但显然不正确。我什至可以将 e.getSource() 与 int 或其他东西进行比较吗?

【问题讨论】:

  • 你可以使用根据GUI增加的静态int
  • 为什么不只存储每个按钮的状态?
  • @ΦXocę웃Пepeúpaツ 你的意思是像一个int计数器?我如何将 (JButton).e.getSource() 与 int 计数器进行比较?
  • @gooroo7 你是什么意思存储状态?按钮是否被按下?我不明白你的意思
  • 不像counter那么简单,但是思路是类似的,每次设置一个按钮时增加全局计数器,设置一个按钮时减少计数器...并且您需要检查计数器是否永远不会超过 14..

标签: java user-interface jbutton


【解决方案1】:

如果您想计算点击次数,您需要创建一个 int(或 long)变量来存储它们,并且只需在 actionPerformed 方法中添加一个 ++ 语句:

private int buttonClicks = 0; // Or public
public void actionPerformed(ActionEvent e)
        {
             if(buttonClicks == 14){
               System.exit(0); // Or a different script
             }else{
             JButton button = (JButton)e.getSource();
             button.setEnabled( false );
             buttonClicks++; // Record click
             }
        }

如果要计算特定按钮的点击次数,则需要创建一个字符串数组,其中包含在构造函数中找到的所有按钮名称:

   JButton jbtn = new JButton("Button") // Button is the name

还有一个单独的int 数组来存储点击。然后您可以使用for 循环来确定按下了哪个按钮,并在 int 数组中增加它的点击次数。

考虑以下示例:

aClass(){

    JFrame jfrm = new JFrame("Example");
    jfrm.setSize(200, 200);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setLayout(new FlowLayout());
    jfrm.setLocationRelativeTo(null);

    JButton jbtn1 = new JButton("Push");
    JButton jbtn2 = new JButton("Click");
    JButton jbtn3 = new JButton("Press");

    jbtn1.addActionListener(this);
    jbtn2.addActionListener(this);
    jbtn3.addActionListener(this);

    jfrm.add(jbtn1);
    jfrm.add(jbtn2);
    jfrm.add(jbtn3);
}

public static String[] buttonNames = {"Push", "Click", "Press"}; // Put button names in an array
public static int[] buttonClicks = {0, 0, 0}; // Set the clicks to default

    public void actionPerformed(ActionEvent ae)
            {
                 for(int i = 0; i < buttons.length; i++){
                    if(ae.getActionCommand().equals(buttonNames[i])){
                    buttonClicks[i] =  buttonClicks[i] + 1; // Record the clicks. I think you can use buttonClicks[i]++, but I'm not sure
                    }
                 }
            }

当您需要访问特定按钮的点击次数时,您可以使用以下内容:

public static int getClicks(String buttonName){
       for(int i = 0; i < aClass.buttonNames.length; i++){
           if(buttonName.equals(aClass.buttonNames[i])){
           return aClass.buttonClicks[i];
           }
       }
}

当您调用该方法时,您所要做的就是将按钮名称作为字符串传递给它。 getClicks("Push");

【讨论】:

  • 这个工作谢谢。我还使用 button.disable(); 使按钮不可点击;
  • 欢迎您!我很乐意提供帮助 =) 而button.disable(); 100% 没问题,我只写了System.exit(0);,因为它是限制或停止某事时使用的默认语句 =P
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多