gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:41:22 24 4
gpt4 key购买 nike

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 则它会播放其中一种声音,否则如果 e.getSource() == test2 它将播放另一种声音,但是没有工作

最佳答案

正如其他人在评论中所说,您的代码运行得很好。

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

这是按预期工作的代码:

enter image description here

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

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

关于java - 我创建了一个 JButton,单击时会播放声音 - 如何在更多按钮上实现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com