从head java中copy一段代码,可是无法编译
eclipse提示button.addActionListener(this)的参数错误,要改成button.addActionListener((ActionListener) this);请问问题在哪?谢谢
下面是code
 public void go(){
     JFrame frame=new JFrame();
     JPanel panel=new JPanel();
     JButton button=new JButton("Just Click it");
        button.addActionListener(this);
     text=new JTextArea(10,20);
     text.setLineWrap(true);
    
     JScrollPane scroller=new JScrollPane(text);
     scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
     scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
     panel.add(scroller);
     frame.getContentPane().add(BorderLayout.CENTER,panel);
     frame.getContentPane().add(BorderLayout.SOUTH,button);
    
     frame.setSize(350,300);
     frame.setVisible(true);
    
    }
    public void actionPerformed(ActionEvent ev){
     text.append("button clicked \n");
    
    }

解决方案 »

  1.   

    答:你确信go()方法所属的类,一定加了:implements java.awt.event.ActionListener吗?
      

  2.   

    可以传this,但是你要写个内部类实现ActionListener接口。
      

  3.   

    还没new ActionListener就敢add?
      

  4.   

    1楼说的很对  go()所在的类实现了ActionListener接口以后,才可以传this给addActionListener方法。
      

  5.   

    需要实现ActionListener接口才可以的