2017-04-02 29 views
0

我想要計算從10 x 10的JButton網格中點擊的JButton的數量。如何計算從10 x 10的JButton網格中點擊了多少個JButtons

這就是我所說的 enter image description here

不管怎麼說,我不知道怎麼算多少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); 
     } 
    } 

這是我製作100個按鍵,給他們每個人一個ActionListener所以點擊一個JButton時,它變得無法點擊forLoops。

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或其他東西進行比較?

+0

您可以使用根據GUI –

+0

增加的靜態int爲什麼不直接存儲每個按鈕的狀態? – gooroo7

+0

@ΦXocę웃Пенео你的意思是像一個int計數器? 我如何比較(JButton).e.getSource()到一個int計數器? –

回答

1

如果你要計算點擊次數,你需要創建一個int(或長期)可變他們在存儲,並且只需在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陣列WIL l存儲點擊。然後你可以使用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");

+0

這一個作品謝謝。另外我通過使用button.disable()使按鈕不可點擊。 –

+0

歡迎您!我很樂意幫忙=)和'button.disable();'是100%好,我只寫了'System.exit(0);',因爲它是限制或停止某事物時使用的默認語句= P –