简体   繁体   English

将ActionListener用于另一个ActionListener

[英]using ActionListener into another ActionListener

I want to use the first ActionListener (About) into the second ActionListener (About2) without copying the first one into teh second one are there any way to do that? 我想将第一个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///////////////
       }
       }
   );

The simplest thing to do here would be to copy the reference of that anonymous object into a temp variable and pass that reference. 这里最简单的方法是将该匿名对象的引用复制到临时变量中并传递该引用。

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);

Another option would be to make your class implement ActionListener and simply do: 另一种选择是让你的类实现ActionListener并简单地执行:

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

While you can do the above as stated in the comment it isn't the best idea. 虽然您可以按照评论中的说明执行上述操作,但这不是最好的主意。 Another option is create another class to implement ActionListener and create an instance of that class. 另一个选项是创建另一个类来实现ActionListener并创建该类的实例。

public class ReusableListener implements ActionListener

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

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

Well as long as you're not using the ActionEvent argument, you could just create a method like this in your class, 好吧,只要你没有使用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);
}

and in your actionPerformed methods, just call 并在你的actionPerformed方法中,只需调用

doAction();

So like this, 所以这样,

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

   );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM