当前位置: 代码迷 >> Java相关 >> 为什么加了一个监听器会出错啊??
  详细解决方案

为什么加了一个监听器会出错啊??

热度:116   发布时间:2007-11-26 22:06:45.0
为什么加了一个监听器会出错啊??
为什么加了一个监听器后,编译器会出现下列错误啊??
lianxi.java:20: 不兼容的类型
找到: <匿名 java.awt.event.MouseAdapter>
需要: java.awt.event.ActionListener
          ActionListener action=new MouseAdapter()
                                ^
1 错误
import java.awt.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.*;
import java.awt.event.*;
class Lianxi extends JFrame
{
   JPanel contentPane;
   
   JCheckBox JC=new JCheckBox("JChecdBox");
   JRadioButton JR=new JRadioButton("JRadioButton");
   JButton JB=new JButton("JButton");
   JToggleButton JT=new JToggleButton("JToggleButton");
   private BasicArrowButton up=new BasicArrowButton(BasicArrowButton.NORTH),
                            down=new BasicArrowButton(BasicArrowButton.SOUTH),
                            right=new BasicArrowButton(BasicArrowButton.EAST),
                            left=new BasicArrowButton(BasicArrowButton.WEST);
   
   ActionListener action=new MouseAdapter()
    {
           public void mouseEntered(MouseEvent e)
           {
             JR.setText("nihao");
           }
    };
   public Lianxi(String str)
   {
      super(str);
        contentPane=(JPanel)getContentPane();
        JPanel jpanel=new JPanel();
        contentPane.setLayout(new FlowLayout());
        jpanel.setBorder(new TitledBorder("Directions"));
        jpanel.add(up);
        jpanel.add(down);
        jpanel.add(right);
        jpanel.add(left);
        contentPane.add(JB);
        contentPane.add(JT);
        contentPane.add(JC);
        contentPane.add(JR);
        contentPane.add(jpanel);
        JR.addActionListener(action);
   }
  public static void main(String[] args)
  {
    Lianxi lianxi=new Lianxi("Hello");
    lianxi.setSize(550,100);
    lianxi.setLocation(180,180);
    lianxi.setVisible(true);
    lianxi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
搜索更多相关的解决方案: 监听  

----------------解决方案--------------------------------------------------------
我把 ActionListener action=new MouseAdapter()
改成 MouseAdapter action=new MouseAdapter()

还有 JR.addActionListener(action);
改成 JR.addMouseListener(action);
可以通过,但不知道这是不是你的目的!
----------------解决方案--------------------------------------------------------
回复 2# 的帖子
是我要的,谢谢你的指点!
----------------解决方案--------------------------------------------------------
  相关解决方案