回 帖 发 新 帖 刷新版面

主题:[讨论]为什么加了一个监听器会出错啊??

为什么加了一个监听器后,编译器会出现下列错误啊??
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);
  }
}

回复列表 (共1个回复)

沙发

JR.addMouseListener(action);

我来回复

您尚未登录,请登录后再回复。点此登录或注册