java 怎么给按钮设置索引?

我想给按钮4和按钮5添加鼠标单击事件,但是我不知道怎么加,求大神给个方法
import java.awt.*;
import javax.swing.*;
public class yt extends JFrame{
static int t;
public static JButton an;

public static void main(String[] args) {
    JFrame j=new JFrame();
    Container c=j.getContentPane();
    JPanel j1=new JPanel();
    j1.setLayout(new FlowLayout(1,10,10));
    for(t=0;t<10;t++){
         an=new JButton("按钮"+t);
         j1.add(an);
    }

    c.add(j1);


    j.setVisible(true);
    j.setSize(400,300);
    j.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

public class yt extends JFrame {
public static void main(String[] args) {
anniu();
}

public static void anniu() {
    JFrame j = new JFrame();
    Container c = j.getContentPane();
    JPanel j1 = new JPanel();
    j1.setLayout(new FlowLayout(1, 10, 10));
    for (int t = 0; t < 10; t++) {
        final JButton an = new JButton("按钮" + t);
        j1.add(an);
        an.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(an.getText());
                if ("按钮4".equals(an.getText())) {
                    System.out.println("按钮4");
                } else if ("按钮5".equals(an.getText())) {
                    System.out.println("按钮5");
                }
            }
        });
    }

    c.add(j1);
    j.setVisible(true);
    j.setSize(400, 300);
    j.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}

你在对象an上添加监听事件就一直会监听按钮9,因为你的对象for循环里面每次都重新给了个地址,所以an的地址其实就是按钮9的地址,改成这样就可以了。

 for(t=0;t<10;t++){
         Button tempb=new JButton("按钮"+t);
                if(t==4){
                    tempb.addActionListener(...);
                }
                    if(t==5){
                    tempb.addActionListener(...);
                }
         j1.add(tempb);
    }

ArrayList<Button> buttons=new ArrayList<Button>();
  for(t=0;t<10;t++){
         Button tempb=new JButton("按钮"+t);
                buttons.add(tempb);
         j1.add(buttons.get(t));
    }
        buttons.get(4).addActionListener(...);

添加一个actionlistener,,事件监听就行了,,

设置按钮属性,查文档找属性