クリックしたJButtonの数を10 x 10 JButtonグリッドから数えたいとします。10 x 10 Java JButtonグリッドからクリックされたJButtonの数をカウントする方法
を話して何ですが、私はクリックされているどのように多くの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などと比較することはできますか?
GUIに応じて増加するスタティックintを使用できます –
各ボタンの状態を保存するだけですか? – gooroo7
@ΦXocen笑Пepeúpaツあなたintカウンターのような意味ですか? (JButton).e.getSource()をintカウンタと比較するにはどうすればよいですか? –