简体   繁体   English

如何计算从10 x 10 java JButton网格中单击了多少个JButton

[英]How to count how many JButtons have been clicked from a 10 x 10 java JButton grid

I want to count the number of JButtons that has been clicked on from a 10 x 10 JButton grid. 我想计算从10 x 10 JButton网格中单击的JButton的数量。

This is what I am talking about 这就是我在说的 在此处输入图片说明

Anyways, I don't know how to count how many JButtons have been clicked. 无论如何,我不知道如何计算已经单击了多少个JButton。 I thought about making 100 JButtons but that seems silly. 我考虑过制作100个JButton,但这似乎很愚蠢。

Also how do I prevent more than 14 buttons from being clicked? 另外,如何防止单击超过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);
       }
    }

This is my forLoops for making 100 buttons and giving each of them an actionListener so when a JButton is clicked, it becomes unclickable. 这是我的forLoop,用于制作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);

        }
    }

I have tried putting in a counter but it is obviously not correct. 我试着放在柜台上,但这显然是不正确的。 Can I even compared e.getSource() to an int or something? 我甚至可以将e.getSource()与int或其他东西进行比较吗?

If you want to count the number of clicks you will need to create an int (or long) variable to store them in, and simply add a ++ statement in the actionPerformed method: 如果要计算点击次数,则需要创建一个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
             }
        }

and if you want to count the number of clicks on a specific button, you will need to create a string array containing all the button names which are found in the constructors: 如果要计算特定按钮的点击次数,则需要创建一个字符串数组,其中包含在构造函数中找到的所有按钮名称:

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

and a separate int array which will store the clicks. 和一个单独的int数组,用于存储点击次数。 Then you can use a for loop to figure out which button was pressed, and increase it's click in the int array. 然后,您可以使用for循环找出按下了哪个按钮,并增加int数组中的单击次数。

Consider the following example: 考虑以下示例:

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
                    }
                 }
            }

and anytime you need to access the number of clicks for a specific button, you could use something like the following: 并且只要您需要访问特定按钮的点击次数,就可以使用类似以下的内容:

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];
           }
       }
}

and when you call that method, all you have to do is pass the button name to it as a string. 当您调用该方法时,您要做的就是将按钮名称作为字符串传递给它。 getClicks("Push");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM