Java jbutton在arraylist中,如何知道单击了哪一个
我有两个大小相同的数组列表,一个用于JButton,一个用于JTextField。单击按钮时,应更新相应的文本字段。我怎么能意识到这一点 p、 jcomponents对(jbutton+jtextfield)的数量不是固定的。它们是为用户添加或删除而设计的。但是,它们必须按固定顺序排列Java jbutton在arraylist中,如何知道单击了哪一个,java,swing,arraylist,jbutton,actionlistener,Java,Swing,Arraylist,Jbutton,Actionlistener,我有两个大小相同的数组列表,一个用于JButton,一个用于JTextField。单击按钮时,应更新相应的文本字段。我怎么能意识到这一点 p、 jcomponents对(jbutton+jtextfield)的数量不是固定的。它们是为用户添加或删除而设计的。但是,它们必须按固定顺序排列 ArrayList<JButton> buttonList = new ArrayList<JButton>(); ArrayList<JTextField>
ArrayList<JButton> buttonList = new ArrayList<JButton>();
ArrayList<JTextField> textFieldList = new ArrayList<JTextField>();
private JButton createButton(){
JButton button = new JButton("Choose File");
buttonList.add(button);
button.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
textField = textFieldList.get(i)
textField.setText("show updats");
}
}
);
return button;
}
ArrayList按钮列表=新建ArrayList();
ArrayList textFieldList=新建ArrayList();
私有JButton createButton(){
JButton按钮=新JButton(“选择文件”);
按钮列表。添加(按钮);
button.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件){
textField=textFieldList.get(i)
setText(“显示更新”);
}
}
);
返回按钮;
}
如何知道JButton数组列表中的索引“I”?有一个,它将是触发事件的JButton
public void actionPerformed(ActionEvent event) {
int i = buttonList.indexOf(event.getSource());
.
.
.
}
具有,这将是触发事件的JButton
public void actionPerformed(ActionEvent event) {
int i = buttonList.indexOf(event.getSource());
.
.
.
}
做一个for循环。例如:for(inti=0;i