我想要計算從10 x 10的JButton網格中點擊的JButton的數量。如何計算從10 x 10的JButton網格中點擊了多少個JButtons
不管怎麼說,我不知道怎麼算多少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或其他東西進行比較?
您可以使用根據GUI –
增加的靜態int爲什麼不直接存儲每個按鈕的狀態? – gooroo7
@ΦXocę웃Пенео你的意思是像一個int計數器? 我如何比較(JButton).e.getSource()到一個int計數器? –