以前我给一个按钮加监听器用的是匿名内部类的方式添加的,比如back.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        frame.setVisible(false);
    }
});我现在有很多按钮,添加监听器的内容是一样的,想象把new ActionListener()拿出来,
但是我ActionListener al=new ActionListener();是错误的,应为ActionListener是个接口谁能教我应该怎么做啊?

解决方案 »

  1.   

    Class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
    }
    }....
    MyListener al=new MyListener();
    back.addActionListener(al);
      

  2.   


    如果非要使用匿名内部类的话,那这样如何?

    ActionListener al = new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
        }
    };
    back.addActionListener(al);
      

  3.   

    如果按钮很多就不要用匿名内部类了..更显得烦琐
    应该用ActionListener接口
    实现抽象方法
    public void actionPerformed(ActionEvent e)
    {
           if()
           else if
           else
    }
      

  4.   


    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class C extends JFrame { private JButton b1, b2, b3; private Container context; public C() {
    this.setTitle("测试");
    this.setVisible(true);
    context = this.getContentPane();
    b1 = new JButton("按钮1");
    b2 = new JButton("按钮2");
    b3 = new JButton("按钮3");
    context.setLayout(new BorderLayout());
    context.add(b1, BorderLayout.NORTH);
    context.add(b2, BorderLayout.CENTER);
    context.add(b3, BorderLayout.SOUTH);

    MyListener al=new MyListener(this);
    b1.addActionListener(al);
    b2.addActionListener(al);
    b3.addActionListener(al); this.setResizable(false);
    this.setBounds(200, 200, 300, 400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args) {
    new C();
    } //成员内部类,class小写
    class MyListener implements ActionListener {

    private JFrame frame;
    public MyListener(JFrame frame){
    this.frame=frame;
    }

    public void actionPerformed(ActionEvent e) {
    frame.setVisible(false);
    }
    }}
      

  5.   

    写一个外部类,实现要添加的监听接口. 
    按钮添加监听如下:
         MyListner myListener=new MyListener();
         JButton btnOK=new JButton("确定");
         btnOK.addActionListner(myListener);
         btnOK.addKeyListener(myListener);外部类代码如下:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;public class MyListener implements ActionListener, KeyListener { public void actionPerformed(ActionEvent e) {
                 if(e.getSource()==btnOK){             }
    } public void keyPressed(KeyEvent e) {
                 if(e.getSource()==btnOK){             }
    } public void keyReleased(KeyEvent e) {
                 if(e.getSource()==btnOK){             }
    } public void keyTyped(KeyEvent e) {
                 if(e.getSource()==btnOK){             }
    }}
      

  6.   

    Class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
    }
    }....
    MyListener al=new MyListener();
    back.addActionListener(al);al可以改成this;
      

  7.   

    简单的方法就是写个类实现ActionListener接口,而且这样便于维护。。