import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class  HelloJava3
 extends  JComponent
  implements MouseMotionListener,ActionListener{
   int mx=145,my=79;  // initial x,y 的坐标
   String theMe;// message
   JButton theButton;   //create someColors arry[]
   int colorIndex;
   static Color[] someColors={
    Color.black,Color.blue,Color.cyan,Color.darkGray,Color.darkGray,Color.green,
    Color.orange
   };   public HelloJava3(String message){
    theButton=new JButton("Change Color");
    theMe =message;
    setLayout(new FlowLayout());
    add(theButton);
    theButton.addActionListener(this);
    addMouseMotionListener(this);   }   public void paintComponent(Graphics g){
   g.drawString("Happy new year",125,100);
   g.drawString("fill",125,95);
   g.drawString(theMe,mx,my);   }  public void  mouseDragged(MouseEvent e){
   mx=e.getX();
   my=e.getY();
   repaint();
  }  public void mouseMoved(MouseEvent e){
   //这里没有代码表示可以忽略这个方法
  }  public void actionPerformed(MouseEvent e){
   if (e.getSource()==theButton)
    changeColor();  }
  // 第一次用sychronized  synchronized private void changeColor(){
   //递增索引
   if (++colorIndex==someColors.length)
    colorIndex=0;
    setForeground(currentColor());
    repaint();
  }  synchronized private Color currentColor(){
   return someColors[colorIndex];  }
  public static void main (String[] args) {
         JFrame f=new JFrame("HelloJava3");
         f.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent we){
           System.exit(0);
          }
         });
         f.setSize(250,250);
         f.getContentPane().add(new HelloJava3("Hello !Java"));
         f.setLocation(500,500);
          f.setVisible(true);      }
}
/*E:\Java\jkk\HelloJava3.java:5: HelloJava3 不是抽象的,并且未覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent)
public class  HelloJava3
*/ //我的程序为什么编译时会出现这个问题?求助!!!!

解决方案 »

  1.   

    不是提示的很明白么?
    你在类的声明中实现了 java.awt.event.ActionListener 接口
    但是却没有实现该接口定义的方法,也没有将类声明为抽象类
      

  2.   

    package Jan;import java.awt.*;
    import java.awt.event.*;import javax.swing.*;public class  HelloJava3
     extends  JComponent
      implements MouseMotionListener,ActionListener{
       int mx=145,my=79;  // initial x,y 的坐标
       String theMe;// message
       JButton theButton;   //create someColors arry[]
       int colorIndex;
       static Color[] someColors={
        Color.black,Color.blue,Color.cyan,Color.darkGray,Color.darkGray,Color.green,
        Color.orange
       };   public HelloJava3(String message){
        theButton=new JButton("Change Color");
        theMe =message;
        setLayout(new FlowLayout());
        add(theButton);
        theButton.addActionListener(this);
        addMouseMotionListener(this);   }   public void paintComponent(Graphics g){
       g.drawString("Happy new year",125,100);
       g.drawString("fill",125,95);
       g.drawString(theMe,mx,my);   }  public void  mouseDragged(MouseEvent e){
       mx=e.getX();
       my=e.getY();
       repaint();
      }  public void mouseMoved(MouseEvent e){
       //这里没有代码表示可以忽略这个方法
      }  public void actionPerformed(MouseEvent e){
       if (e.getSource()==theButton)
        changeColor();  }
      // 第一次用sychronized  synchronized private void changeColor(){
       //递增索引
       if (++colorIndex==someColors.length)
        colorIndex=0;
        setForeground(currentColor());
        repaint();
      }  synchronized private Color currentColor(){
       return someColors[colorIndex];  }
      public static void main (String[] args) {
             JFrame f=new JFrame("HelloJava3");
             f.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent we){
               System.exit(0);
              }
             });
             f.setSize(250,250);
             f.getContentPane().add(new HelloJava3("Hello !Java"));
             f.setLocation(500,500);
              f.setVisible(true);      }public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    }
    }