Java 将actionListener添加到许多JButton中

Java 将actionListener添加到许多JButton中,java,eclipse,user-interface,actionlistener,Java,Eclipse,User Interface,Actionlistener,我想将actionListener添加到位于BigPanel内的各种按钮中。actionListener用于deleteAction方法。在我给出的代码中,我只将actionListener添加到一个按钮BTNBleedingPreventions。现在,我想将actionListener添加到BigPanel中的所有按钮中,其中按钮被计算为20+。是否有任何方法可以用来添加actionListener,而不是将每个按钮 buttonName.addActionListener(deleteAc

我想将actionListener添加到位于BigPanel内的各种按钮中。actionListener用于deleteAction方法。在我给出的代码中,我只将actionListener添加到一个按钮BTNBleedingPreventions。现在,我想将actionListener添加到BigPanel中的所有按钮中,其中按钮被计算为20+。是否有任何方法可以用来添加actionListener,而不是将每个按钮

buttonName.addActionListener(deleteAction)

?


创建制作/添加按钮的方法:

protected JButton createButton( JPanel panel, String text, 
                                String position, ActionListener listener ) {
   JButton b = new JButton(text);
   panel.add( b, position );
   if ( listener != null ) {
      b.addActionListener(listener);
   }
   return b;
}
然后使用此方法创建按钮。例如:

JButton btnAntithrombotics = createButton( bigPanel, "Antithrombotics",
                                "2, 14", deleteAction);
如果不想将侦听器添加到该按钮,可以将null作为最后一个参数传递


注意:惯例是以小写字母开头变量名,因此“bigPanel”而不是“bigPanel”。

创建一个方法来制作/添加按钮:

protected JButton createButton( JPanel panel, String text, 
                                String position, ActionListener listener ) {
   JButton b = new JButton(text);
   panel.add( b, position );
   if ( listener != null ) {
      b.addActionListener(listener);
   }
   return b;
}
然后使用此方法创建按钮。例如:

JButton btnAntithrombotics = createButton( bigPanel, "Antithrombotics",
                                "2, 14", deleteAction);
如果不想将侦听器添加到该按钮,可以将null作为最后一个参数传递


注意:惯例是以小写字母开头变量名,因此“bigPanel”而不是“bigPanel”。

创建一个方法来制作/添加按钮:

protected JButton createButton( JPanel panel, String text, 
                                String position, ActionListener listener ) {
   JButton b = new JButton(text);
   panel.add( b, position );
   if ( listener != null ) {
      b.addActionListener(listener);
   }
   return b;
}
然后使用此方法创建按钮。例如:

JButton btnAntithrombotics = createButton( bigPanel, "Antithrombotics",
                                "2, 14", deleteAction);
如果不想将侦听器添加到该按钮,可以将null作为最后一个参数传递


注意:惯例是以小写字母开头变量名,因此“bigPanel”而不是“bigPanel”。

创建一个方法来制作/添加按钮:

protected JButton createButton( JPanel panel, String text, 
                                String position, ActionListener listener ) {
   JButton b = new JButton(text);
   panel.add( b, position );
   if ( listener != null ) {
      b.addActionListener(listener);
   }
   return b;
}
然后使用此方法创建按钮。例如:

JButton btnAntithrombotics = createButton( bigPanel, "Antithrombotics",
                                "2, 14", deleteAction);
如果不想将侦听器添加到该按钮,可以将null作为最后一个参数传递


注意:惯例是以小写字母开始变量名,因此“bigPanel”而不是“bigPanel”。

创建所有按钮后,您可以循环bigPanel中的所有组件,并在其中添加操作侦听器:

for(Component c : BigPanel.getComponents()){
    if(c instanceof JButton)//check if the component is a button
        ((JButton)c).addActionListener(listener);//cast to a JButton and add the action listener.
}

创建所有按钮后,您可以在BigPanel中循环所有组件,并在其中添加操作侦听器:

for(Component c : BigPanel.getComponents()){
    if(c instanceof JButton)//check if the component is a button
        ((JButton)c).addActionListener(listener);//cast to a JButton and add the action listener.
}

创建所有按钮后,您可以在BigPanel中循环所有组件,并在其中添加操作侦听器:

for(Component c : BigPanel.getComponents()){
    if(c instanceof JButton)//check if the component is a button
        ((JButton)c).addActionListener(listener);//cast to a JButton and add the action listener.
}

创建所有按钮后,您可以在BigPanel中循环所有组件,并在其中添加操作侦听器:

for(Component c : BigPanel.getComponents()){
    if(c instanceof JButton)//check if the component is a button
        ((JButton)c).addActionListener(listener);//cast to a JButton and add the action listener.
}

非常感谢你,先生。我使用了你提供的代码,它工作正常。但我不清楚你关于“约定是以小写字母开头变量名,所以用“bigPanel”代替“bigPanel”的说法,因为我用bigPanel代替了bigPanel,它仍然有效。@哆啦a梦,这是“约定”,意思是人们通常使用的,而不是只起作用的。哦,我明白了。谢谢,先生,非常感谢。我使用了你提供的代码,它工作正常。但我不清楚你关于“约定是以小写字母开头变量名,所以用“bigPanel”代替“bigPanel”的说法,因为我用bigPanel代替了bigPanel,它仍然有效。@哆啦a梦,这是“约定”,意思是人们通常使用的,而不是只起作用的。哦,我明白了。谢谢,先生,非常感谢。我使用了你提供的代码,它工作正常。但我不清楚你关于“约定是以小写字母开头变量名,所以用“bigPanel”代替“bigPanel”的说法,因为我用bigPanel代替了bigPanel,它仍然有效。@哆啦a梦,这是“约定”,意思是人们通常使用的,而不是只起作用的。哦,我明白了。谢谢,先生,非常感谢。我使用了你提供的代码,它工作正常。但我不清楚你关于“约定是以小写字母开头变量名,所以用“bigPanel”代替“bigPanel”的说法,因为我用bigPanel代替了bigPanel,它仍然有效。@哆啦a梦,这是“约定”,意思是人们通常使用的,而不是只起作用的。哦,我明白了。谢谢,罗迪特先生