1

我想将第一个 ActionListener (About) 用于第二个 ActionListener (About2) 而不将第一个 ActionListener 复制到第二个中,有什么办法吗?

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///////////////
       }
       }
   );
4

2 回答 2

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) ;
于 2012-10-17T11:19:39.263 回答
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();

   );
于 2012-10-17T11:16:12.557 回答