类不是抽象的,不会覆盖抽象方法

时间:2015-04-16 21:47:51

标签: java swing abstract-class actionlistener abstract

我已经用抽象方法阅读了非抽象类的大多数其他问题,我认为我不理解任何问题。所以继承我的问题:

我正在尝试制作一个节拍器,因为节拍器“点击”并播放声音我从http://www.cs.cmu.edu/~illah/CLASSDOCS/javasound.pdf复制了代码。

以下代码是一个创建JFrame,JPanel和JButtton的超级基本程序。单击Button(ClickForSound)时,它应播放声音。

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.applet.*;

class PlsWork implements ActionListener{
 JPanel JPanelThing = new JPanel();

 public PlsWork() {
  //make frame to hold everything
  JFrame Frame4HoldinSheit = new JFrame("Sound Player");

  //set up frame
  Frame4HoldinSheit.setLayout(new FlowLayout());
  Frame4HoldinSheit.setSize(300, 300);
  Frame4HoldinSheit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // make button for playing sound
  JButton ClickForSound = new JButton("Click Me");
  ClickForSound.addActionListener(this);

  //add button to panel, then panel to frame
  Frame4HoldinSheit.add(JPanelThing);
  JPanelThing.add(ClickForSound);
 }

 public void actionPreformed(ActionEvent ae) {
  //if button is pressed
  if(ae.getActionCommand().equals("Click Me")){
    try {
        //make a AudioClip 
        AudioClip MetSound = Applet.newAudioClip(
            //Using this file (FOUND HERE)
            new  URL( "C:/Users/jheld7499/desktop/4d.wav" ));
            MetSound.play();
    } catch (java.net.MalformedURLException murle) {
        //ya done goofed on where u thought the file was
        System.out.println("Malformed URL Exception"); }
  }
 }

 public static void main(String args[]){
  SwingUtilities.invokeLater(new Runnable() { public void run() {
      //run the program
      new PlsWork();} });
 }
}

我得到的错误是Class PlsWork不是抽象的,因此不会覆盖抽象方法PlsWork

sound.java:8: error: PlsWork is not abstract and does not override 
abstract method actionPerformed(ActionEvent) in ActionListener
class PlsWork implements ActionListener{
^
1 error

另外请跟我说话就像一个白痴,因为我几乎不了解抽象的意思或如何使方法抽象,如果我想要任何东西是抽象的或其他任何东西。

1 个答案:

答案 0 :(得分:3)

检查语法:actionPerformed != actionPreformed。添加@Override注释在其他非编译时错误情况下可能会有所帮助(例如,您正在覆盖子类的非抽象方法)

@Override
public void actionPerformed(ActionEvent e){

}