一直在此停留一段時間,並在思考了一下搜索後發現我找不到的東西會詢問是否有人對我的問題有解決方案。目前,我正在研究一個小型拼貼項目,我需要一個包含100個按鈕的面板,但每個按鈕都必須有一個動作偵聽器。選擇此動作偵聽器時,必須在網格中報告其編號並更改按鈕的文本。ArrayList <JButton>使用ArrayList添加Action偵聽器
for (int i = 0; i < 100; ++i) //Sets buttons created
{
ArrayList<JButton> testButton = new ArrayList<JButton>(); //Button Text
PlayerGrid1.add(new JButton(" ? "));
}
的代碼是我如何添加按鈕到ArrayList,但我遇到的問題是,當我嘗試添加一個動作監聽,它拋出關於抽象的按鈕和其他問題的錯誤。
JPanel PlayerGrid1 = new JPanel();
PlayerGrid1.setBackground(Color.WHITE);
PlayerGrid1.setBounds(0, 0, 375, 400);
frmBattleships.getContentPane().add(PlayerGrid1);
PlayerGrid1.setLayout(new GridLayout(10, 10, 0, 0));
這是我存儲按鈕的網格。
如果有人知道如何將偵聽器添加到ArrayList中,或者鏈接到使用與我相同的方法的某個人的帖子,那麼將不勝感激。也只是爲了讓任何人知道,如果這沒有正確或錯誤設置請不要火焰我通常不會問許多堆棧溢出的問題。謝謝。