actionlistener不工作,声音没有播放但没有错误

时间:2014-03-16 12:07:00

标签: java actionlistener

我使用的声音没有播放,也没有使用actionlistener ..我在wav中有声音剪辑,它在课程目录中..请帮助我..我不明白我的编码有什么问题

public class Willy extends JApplet implements ActionListener
{/**
 * 
 */
    private static final long serialVersionUID = 1L;

    Button play,stop;
    Container pane=getContentPane();
    AudioClip audioClip;
    Date date = new Date();//new command 4
    Timer timer = new Timer (1000,this);
    int eye1=50,eye2=45,pupil=10;
    int r=255,g=255,b=255;
    Color white=new Color(255,255,255);
    Color black=new Color(0,0,0);
    JLabel label;


    public void paint(Graphics g)
    {
    setSize(1000,1000);
    setLayout(null);

        Graphics2D g2 = (Graphics2D) g;//new command 2
        g2.setStroke(new BasicStroke(6));

            g.drawOval(83,6,321,303);//outer head
            g.setColor(new Color(255,102,0));//orange
            g.fillOval(83,6,321,303);//outer head color
            g.setColor(Color.BLACK);//black
            g.drawLine(151,282,93,391);//left arm outer
            g.drawArc(93,350,60,80,180,125);//left hand arc
            g.drawLine(93,390,140,425);//left hand line
            g.drawLine(140,425,155,395);//left arm inside
            g.drawLine(335,282,365,391);//right arm outer
    g.drawOval(335,387,40,40);//right hand
    g.drawArc(335,390,20,20,35,-105);//right finger
    g.drawLine(336,415,325,385);//right arm inside
    g.drawLine(150,405,150,435);//left torso
    g.drawLine(330,400,330,435);//right torso
    g.drawLine(150,435,330,435);//bottom torso
    g.drawLine(240,435,240,410);//zipper
    g.drawLine(160,450,320,450);//bottom leg
    g.drawLine(160,435,160,450);//left leg
    g.drawLine(320,435,320,450);//right leg
    g.drawLine(160,450,150,465);//left foot
    g.drawLine(320,450,330,465);//right foot
    g.drawLine(150,465,330,465);//bottom foot
    g.drawOval(125,40,235,235);//inner head
    g.setColor(new Color(139,69,19));//brown
    g.fillOval(155,65,180,180);//smallest head color
    g.setColor(Color.BLACK);
    g.drawOval(155,65,180,180);//smallest head
    g.setColor(new Color(255,228,196));//hoodie hole color
    g.fillOval(195,65,100,180);//hoodie hole color
    g.setColor(Color.BLACK);
    g.drawOval(195,65,100,180);//hoodie hole
    g.setColor(Color.BLACK);
    g.drawOval(195,115,eye1,eye2);//left eye
    g.drawOval(245,115,eye1,eye2);//right eye
    g.setColor(white);
    g.fillOval(195,115,eye1,eye2);
    g.fillOval(245,115,eye1,eye2);
    g.setColor(black);
    g.fillOval(215,135,10,10);//left pupil color
    g.fillOval(265,135,10,10);//right pupil color
    g.drawOval(215,135,pupil,pupil);//left pupil
    g.drawOval(265,135,pupil,pupil);//right pupil

    Polygon x=new Polygon();//torso color  new command 1
    x.addPoint(151,182);
    x.addPoint(93,391);
    x.addPoint(140,425);
    x.addPoint(155,395);
    x.addPoint(150,405);
    x.addPoint(150,435);
    x.addPoint(330,435);
    x.addPoint(330,400);
    x.addPoint(325,385);
    x.addPoint(336,415);

    Polygon leg=new Polygon();
    leg.addPoint(150,435);
    leg.addPoint(330,435);
    leg.addPoint(330,450);
    leg.addPoint(160,450);
    leg.addPoint(160,435);

    Polygon q=new Polygon();
    q.addPoint(342,308);
    q.addPoint(335,282);
    q.addPoint(332,285);
    q.addPoint(329,288);
    q.addPoint(326,289);
    q.addPoint(323,291);
    q.addPoint(320,293);
    q.addPoint(318,294);
    q.addPoint(315,296);
    q.addPoint(313,297);
    q.addPoint(312,299);
    q.addPoint(310,299);
    q.addPoint(307,300);
    q.addPoint(303,301);
    q.addPoint(300,303);
    q.addPoint(294,305);
    q.addPoint(279,307);

    Polygon h=new Polygon();
    h.addPoint(336,415);
    h.addPoint(325,385);
    h.addPoint(325,415);

    Polygon t=new Polygon();
    t.addPoint(336,415);
    t.addPoint(310,365);
    t.addPoint(345,360);
    t.addPoint(342,315);
    t.addPoint(365,391);
    t.addPoint(361,388);
    t.addPoint(358,385);

    Polygon foot=new Polygon();
    foot.addPoint(320,450);
    foot.addPoint(330,465);
    foot.addPoint(150,465);
    foot.addPoint(160,450);

    g.setColor(Color.BLUE);

    g.setColor(new Color(255,102,0));
    g.fillPolygon(x);
    g.fillPolygon(t);
    g.fillRect(200,308,125,100);
    g.fillRect(220,308,122,70);
    g.fillPolygon(h);
    g.fillPolygon(q);
    g.fillPolygon(leg);
    g.setColor(new Color(139,69,19));
    g.fillOval(335,387,40,40);
    g.fillArc(93,350,60,80,180,125);
    g.fillPolygon(foot);

    // TUNOG

    play = new Button("  Play   ");

    //Set Location and Size
    play.setLocation(800, 200);
    play.setSize(50,50);
    //Add sa Applet para makita
    add(play);

    //Add Listener sa button para mag karoon sya ng saysay
    play.addActionListener(this);

    stop = new Button("  Stop  ");
    stop.setLocation(800, 250);
    stop.setSize(50,50);
    add(stop);
    stop.addActionListener(this);
    //audioClip = getAudioClip(getCodeBase(),"kenny_1.au");

}



public void actionPerformed(ActionEvent e)
{
     Button source = (Button)e.getSource();
      if (source.getLabel().equals("  Play   ")){
         audioClip.play();
         eye2=5;
         pupil=5;
         white=new Color(0,0,0);
         black=new Color(255,228,196);
         JOptionPane.showMessageDialog(null,date.toString());
         repaint();
      }
      else if(source.getLabel() .equals("  Stop  ")){
      audioClip.stop();
      eye2=50;
      pupil=10;
      white=new Color(255,255,255);
      black=new Color(0,0,0);
      repaint();//new command 3
      }

}

}

1 个答案:

答案 0 :(得分:0)

您的getCodeBase()检查项目的Bin文件夹。因此,请确保您要播放的文件存在于Bin文件夹中。

我用以下代码测试了它:

import java.applet.AudioClip;
import javax.swing.JApplet;

@SuppressWarnings("serial")
public class audioPlayer extends JApplet{

    AudioClip aClip;

    public void init(){
        System.out.print("Getting audio clip!");
        aClip = getAudioClip(getCodeBase(), "tetris.wav");
        System.out.print("Looping audio clip!");
        aClip.loop();
    }

}

上面的代码完美无瑕,完美播放声音。