将ActionListener用于另一个ActionListener

时间:2012-10-17 11:14:09

标签: java actionlistener

我想将第一个ActionListener(About)用于第二个ActionListener(About2) 没有将第一个复制到第二个,有没有办法做到这一点?

About.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
          AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
       }
       }
   );



About2.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){

                  ////////code here///////////////
       }
       }
   );

2 个答案:

答案 0 :(得分:2)

这里最简单的方法是将该匿名对象的引用复制到临时变量中并传递该引用。

ActionListener temp= new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
          AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
       }
       };

About.AddActionListener(temp);
About2.AddActionListener(temp);

另一种选择是让你的类实现ActionListener并简单地执行:

About.AddActionListener(this)
About2.AddActionListener(this);

虽然您可以按照评论中的说明执行上述操作,但这不是最好的主意。 另一个选项是创建另一个类来实现ActionListener并创建该类的实例。

public class ReusableListener implements ActionListener

ActionListener listener = new ReusableListener() ;//as a field

About.addActionListener(listener) ;
About2.addActionListener(listener) ;

答案 1 :(得分:1)

只要你没有使用ActionEvent参数,你就可以在你的班级中创建这样的方法,

public void doAction() {
 AboutMe.setLocation(470,250);
          AboutMe.setSize(400, 200);
          AboutMe.setVisible(true);
          AboutMe.setResizable(false);
          AboutMe.add(panel5);
          panel5.setLayout(null);
          panel5.add(ta);

         ta.setBounds(15, 15, 350, 130);
         ta.setBorder(BorderFactory.createEtchedBorder());
         ta.setText("...................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................\n"
                 +  "....................................");
         ta.setEditable(false);
}

在您的actionPerformed方法中,只需致电

即可
doAction();

就像这样,

About.addActionListener(new ActionListener(){
        @Override
       public void actionPerformed(ActionEvent ae){
doAction();

   );