2017-04-02 7 views
0

クリックしたJButtonの数を10 x 10 JButtonグリッドから数えたいとします。10 x 10 Java JButtonグリッドからクリックされたJButtonの数をカウントする方法

これは私がとにかくについて enter image description here

を話して何ですが、私はクリックされているどのように多くのJButtonがカウントする方法を知りません。私は100 JButtonsを作ることを考えましたが、それはばかげているようです。

また、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); 
     } 
    } 

これはJButtonのがクリックされたときに、それはunclickableなり100個のボタンを作成し、それらのそれぞれにActionListenerを与えるために私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に応じて増加するスタティックintを使用できます –

+0

各ボタンの状態を保存するだけですか? – gooroo7

+0

@ΦXocen笑Пepeúpaツあなた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配列ウィル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();' –

関連する問題