java - 在addActionListener中找不到文件名,JAVA

标签 java function button jframe actionlistener

我正在尝试创建一个在用户单击按钮时播放音频的函数。问题是,当我尝试使用“fileName.addActionListener”从参数传递文件名时,它说无法找到文件名,即使它在参数中引用也是如此。我做错了什么,我该如何解决这个问题?谢谢。

package sunaudiodemo;

import static java.awt.Color.blue;
import static java.awt.Color.green;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import sun.audio.*;    //import the sun.audio package
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class SunAudioDemo {


    public static void playAudioOnClick (final String fileName) {

        //******************************************* 
    fileName.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        try {
            playAudio(fileName);
        } catch (Exception ex) {
            Logger.getLogger(SunAudioDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    });
    //*******************************************

    }

    public static void playAudio (String text) throws Exception {

        // identify the sound file as a File class object
        File soundFile = new File(text);

        // Open an input stream for the File object soundFile
        // This allows Java to read the file.
        InputStream inFile = new FileInputStream(soundFile);

        // Create an AudioStream from the input stream.
        // This tells Java to read the incoming data as sound data.
        AudioStream audio = new AudioStream(inFile);

        // play the sound file using the start method from Audioplayer.player
        AudioPlayer.player.start(audio);

    }

    public static void main(String[] args) throws Exception {


 // create a frame to hold our components
 JFrame myJFrame = new JFrame();
 // create a new a grid layout for the frame - 5 rows x 2 cols, gaps=20
 GridLayout myLayout = new GridLayout(5,2);
 myLayout.setHgap(20);
 myLayout.setVgap(20);

 // assign myLayout to be the layout for MyJFrame
 myJFrame.setLayout(myLayout);

 // Create a button with text OK
 JButton wav1 = new JButton("Play Audio 1");
 myJFrame.add(wav1); // Add the OK button
 playAudioOnClick("wav2.wav");


 // set the title, size, location and exit behavior for the JFrame
 myJFrame.setTitle("Play Audio");
 myJFrame.setSize(360, 480);
 myJFrame.setLocation(200, 100);
 myJFrame.getContentPane().setBackground( green );
 myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 // make the frame visible (activate the frame)
 myJFrame.setVisible(true); 



    } // end main()

} // end class SunAudioDemo

最佳答案

在上面的代码中,fileName 被定义为String。假设这是 java.lang.String ,没有方法 addActionListener()。您需要传入对按钮的引用,以便可以向其中添加 ActionListener。

关于java - 在addActionListener中找不到文件名,JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31753370/

相关文章:

java - 从 JPA 调用 oracle 函数

java - 如何让notify()在wait()之前执行,我尝试使用sleep()但仍然不起作用

使用 JBoss/Spring 和 NetBeans 部署 WebAppp 时出现 javax.naming.NameNotFoundException

Java 数组列表和对象传递

html - 按钮之间的空间以适合页面大小

HTML-如何让按钮在旁边的按钮被移除后保持在固定位置?

java - 在 JButton 上设置多个图标

java - INRIA SPOON API MethodCalls - 检索 method.getExecutable() 和 method.getTarget() 的被调用者类

来自《Kivy Blueprints》一书的 Python kivy 代码

sql - 计算postgres中数据频率的函数