Java ArrayList<;JButton>;使用ArrayList添加操作侦听器

Java ArrayList<;JButton>;使用ArrayList添加操作侦听器,java,arraylist,jbutton,actionlistener,Java,Arraylist,Jbutton,Actionlistener,有一段时间我一直在思考这个问题,经过一段时间的搜索,没有找到我要找的东西,我会问是否有人能解决我的问题。目前,对于一个小拼贴项目,我需要有一个面板,其中包含100个按钮,但每个按钮必须有一个动作监听器。选中此操作侦听器时,必须在网格中报告其编号并更改按钮的文本 for (int i = 0; i < 100; ++i) //Sets buttons created { ArrayList<JButton> testButton = new ArrayLi

有一段时间我一直在思考这个问题,经过一段时间的搜索,没有找到我要找的东西,我会问是否有人能解决我的问题。目前,对于一个小拼贴项目,我需要有一个面板,其中包含100个按钮,但每个按钮必须有一个动作监听器。选中此操作侦听器时,必须在网格中报告其编号并更改按钮的文本

for (int i = 0; i < 100; ++i) //Sets buttons created
    {
        ArrayList<JButton> testButton = new ArrayList<JButton>(); //Button Text
        PlayerGrid1.add( new JButton(" ? ") );
    }
这是我存储按钮的网格


如果有人知道我如何在ArrayList中添加一个监听器,或者有一个链接指向使用与我相同方法的人的帖子,我们将不胜感激。另外,为了让任何人知道这是否是正确的或错误的,请不要点燃我通常不会问很多堆栈溢出问题。谢谢。

为循环定义映射而不是列表,如:

Map<String,JButton> buttonMap = new HashMap<String,JButton>();
Map按钮Map=newhashmap();
之后,您应该为循环中的每个按钮设置唯一的操作命令。“i”可用于此目的

for (int i = 0; i < 100; ++i) //Sets buttons created
{
JButton button = new JButton();
button.setActionCommand(String.valueOf(i));
button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              buttonMap.get(e.getActionCommand()).setText("Whatever you want!");
            }
        });
buttonMap.put(String.valueOf(i), button);
PlayerGrid1.add(button);
}
for(int i=0;i<100;++i)//设置创建的按钮
{
JButton button=新JButton();
setActionCommand(String.valueOf(i));
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
buttonMap.get(例如getActionCommand()).setText(“您想要什么都行!”);
}
});
buttonMap.put(字符串.valueOf(i),按钮);
PlayerGrid1.添加(按钮);
}
试试这个`

    JFrame frmBattleships = new JFrame();
    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));
    for (int i = 0; i < 100; ++i) // Sets buttons created
    {
        ArrayList<JButton> testButton = new ArrayList<JButton>(); // Button
        JButton newButton = new JButton("" + i); // Text
        newButton.setName("" + i);
        newButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(((JButton) e.getSource()).getName());

            }
        });
        PlayerGrid1.add(newButton);

    }
    frmBattleships.setVisible(true);

`
JFrame frmBattleships=newjframe();
JPanel PlayerGrid1=新的JPanel();
PlayerGrid1.挫折背景(颜色:白色);
PlayerGrid1.退步(0,0375400);
frmBattleships.getContentPane().add(PlayerGrid1);
PlayerGrid1.setLayout(新的GridLayout(10,10,0,0));
for(int i=0;i<100;++i)//设置创建的按钮
{
ArrayList testButton=新建ArrayList();//按钮
JButton newButton=newjbutton(“+i);//文本
newButton.setName(“+i”);
addActionListener(newActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(((JButton)e.getSource()).getName());
}
});
PlayerGrid1.添加(新按钮);
}
frmBattleships.setVisible(true);
`
    JFrame frmBattleships = new JFrame();
    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));
    for (int i = 0; i < 100; ++i) // Sets buttons created
    {
        ArrayList<JButton> testButton = new ArrayList<JButton>(); // Button
        JButton newButton = new JButton("" + i); // Text
        newButton.setName("" + i);
        newButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(((JButton) e.getSource()).getName());

            }
        });
        PlayerGrid1.add(newButton);

    }
    frmBattleships.setVisible(true);

`