我创建了一个JButton,它在点击时播放声音 - 我如何在更多按钮上实现它

时间:2016-04-04 05:04:43

标签: java

package pkgnew;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class bob
{
JButton test = new JButton("");
JButton test1 = new JButton("");
JButton test2 = new JButton("");
JButton test3 = new JButton("");
JPanel panel = new JPanel();

public void playSound(String soundName)
 {
   try 
   {
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
   }
   catch(Exception ex)
   {
     System.out.println("Error with playing sound.");
     ex.printStackTrace( );
   }
 }    

 public bob() {

    JFrame frame = new JFrame("Movie Stars");
    test.setIcon(new ImageIcon("C:\\Users\\bob\\Desktop\\Leonardo.jpg"));
    test1.setIcon(new ImageIcon("C:\\Users\\bob\\Desktop\\Bruce.jpg"));
    test2.setIcon(new ImageIcon("C:\\Users\\bob\\Desktop\\Jim.jpg"));
    test3.setIcon(new ImageIcon("C:\\Users\\bob\\Desktop\\Robert.jpg"));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.add(panel);
    frame.setSize(1000, 800);
    frame.setResizable(true);
    frame.setVisible(true);

    panel.add(test);
    panel.add(test1);
    panel.add(test2);
    panel.add(test3);

    panel = new JPanel(new GridLayout(2,2));

    test.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
         playSound("Leonardo.wav");

        }  
    }
  );
}  

public static void main(String[] args) 
{

    bob t = new bob();
} 
}

所以基本上我有一个带有4个图片按钮的网格,当你点击每张图片播放声音时,我正试图这样做。我为其中一个按钮实现了它,但到目前为止我尝试过的所有内容都使其适用于其他3个按钮并没有成功。我以为我只会复制test.addActionListener(new ActionListener() {并将'test'替换为'test1',但不是这样我也尝试在该播放循环中添加'else if catch',这样如果e.getSource() == test那么它会播放其中一个声音,else if e.getSource() == test2它会播放另一个声音但不起作用

2 个答案:

答案 0 :(得分:2)

正如其他人在评论中所说,你的代码工作得很好。

我下载Netbeans只是为了检查一下:p

以下是按预期工作的代码:

enter image description here

这是我使用的ActionListener代码(用2种不同的声音测试):

test.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
     playSound("C:\\Users\\DarkTussin\\Music\\Beep.wav");
     JOptionPane.showMessageDialog(null, "Beep1 Just Went Off");
    }  
});
test1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
     playSound("C:\\Users\\DarkTussin\\Music\\Beep2.wav");
     JOptionPane.showMessageDialog(null, "Beep2 Just Went Off");
    }  
});
test2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
     playSound("C:\\Users\\DarkTussin\\Music\\Beep.wav");
     JOptionPane.showMessageDialog(null, "Beep3 (Beep.wav) Just Went Off");
    }  
});
test3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
     playSound("C:\\Users\\DarkTussin\\Music\\Beep.wav");
     JOptionPane.showMessageDialog(null, "Beep4 (Beep.wav) Just Went Off");
    }  
});

答案 1 :(得分:1)

我做了几乎相同的事情,它也适用于我。我也在声音加载方面做了一些改进。

package helloworld;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by matt on 4/4/16.
 */
public class PlaySounds {
    Map<String, Clip> sounds = new HashMap<>();
    public PlaySounds(){
        loadResources("A.wav");
        loadResources("B.wav");
    }

    public void loadResources(String soundName){
        try
        {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            sounds.put(soundName, clip);
        }
        catch(Exception ex)
        {
            System.out.println("Error with playing sound.");
            ex.printStackTrace( );
        }
    }
    public static void main(String[] args){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JButton a = new JButton("A");
        JButton b = new JButton("B");

        PlaySounds sounds = new PlaySounds();

        a.addActionListener(evt->{
            sounds.playSound("A.wav");
        });

        b.addActionListener(evt->{
            sounds.playSound("B.wav");
        });

        panel.add(a, BorderLayout.WEST);
        panel.add(b, BorderLayout.EAST);

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public void playSound(String soundName){
        Clip c = sounds.get(soundName);
        c.setFramePosition(0);
        c.start();
    }
}

不是每次点击时再次加载声音,我在启动程序时加载了声音。

在标题中回答您的问题。我添加了两个动作监听器,每个按钮播放不同的声音。