gpt4 book ai didi

Java 小程序 : Basic Drum Set

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:43 26 4
gpt4 key购买 nike

我正在尝试编写一个有四个按钮的小程序,所有按钮都播放一个简短的音频文件。目标是尝试让用户成功单击按钮任意多次,从而创建节拍。这是我的尝试:

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


public class drumKit extends JApplet
{

private JButton snareButton;
private JButton hiHatButton;
private JButton bassButton;
private JButton cymbalsButton;
private AudioClip snare;
private AudioClip hiHat;
private AudioClip bass;
private AudioClip cymbals;

public void init()
{
setLayout (new FlowLayout());

sampleButtons();

snare = getAudioClip(getDocumentBase(), "Snare.wav");
hiHat = getAudioClip(getDocumentBase(), "HiHat.wav");
bass = getAudioClip(getDocumentBase(), "Kick.wav");
cymbals = getAudioClip(getDocumentBase(), "Crash.wav");

}

private void sampleButtons()
{
snareButton = new JButton("Snare");
hiHatButton = new JButton("Hi Hat");
bassButton = new JButton("Kick");
cymbalsButton = new JButton("Cymbals");

snareButton.addActionListener(new ButtonListener());
hiHatButton.addActionListener(new ButtonListener());
bassButton.addActionListener(new ButtonListener());
cymbalsButton.addActionListener(new ButtonListener());

add(snareButton);
add(hiHatButton);
add(bassButton);
add(cymbalsButton);
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == snareButton)
snare.play();
if (e.getSource() == hiHatButton)
hiHat.play();
if (e.getSource() == bassButton)
bass.play();
if (e.getSource() == cymbalsButton)
cymbals.play();



}
}
}

问题是,当我单击按钮时,没有任何内容播放。我引用了列出的解决方案here ,会弹出一个窗口,阻止与小程序进行任何进一步的交互。抱歉,这里是个新手。//感谢您的帮助。

最佳答案

当你说“小程序或 GUI”时,我认为你真正指的是小程序或应用程序——它们都是 GUI。我不太熟悉 AudioClip,但如果它工作起来像看起来那么简单,那么您需要做的就是将 JApplet 更改为 JPanel,然后创建一个 main 方法,该方法创建一个 JFrame 并将其内容 Pane 设置为您的 JPanel:

免责声明:此代码尚未经过测试,可能包含编译错误(我将更正指出的任何内容)。

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


public class drumKit extends JPanel implements ActionListener
{

private final JButton snareButton;
private final JButton hiHatButton;
private final JButton bassButton;
private final JButton cymbalsButton;
private final AudioClip snare;
private final AudioClip hiHat;
private final AudioClip bass;
private final AudioClip cymbals;

public drumKit()
{
super();

// create buttons
snareButton = new JButton("Snare");
hiHatButton = new JButton("Hi Hat");
bassButton = new JButton("Kick");
cymbalsButton = new JButton("Cymbals");

// setup audio clips
snare = getAudioClip(getDocumentBase(), "Snare.wav");
hiHat = getAudioClip(getDocumentBase(), "HiHat.wav");
bass = getAudioClip(getDocumentBase(), "Kick.wav");
cymbals = getAudioClip(getDocumentBase(), "Crash.wav");

// set layout
setLayout (new FlowLayout());

// add this action listener to the buttons and add to this panel
sampleButtons();
}

private void sampleButtons()
{
// add this as the each button's action listener
snareButton.addActionListener(this);
hiHatButton.addActionListener(this);
bassButton.addActionListener(this);
cymbalsButton.addActionListener(this);

// add each button to this panel
this.add(snareButton);
this.add(hiHatButton);
this.add(bassButton);
this.add(cymbalsButton);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == snareButton)
snare.play();
else if (e.getSource() == hiHatButton)
hiHat.play();
else if (e.getSource() == bassButton)
bass.play();
else if (e.getSource() == cymbalsButton)
cymbals.play();
}

/**
* main method creates a frame which contains this custom panel
* and displays it.
*/
public static void main(String ...args){
// set the look and feel to the system's look and feel
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
// if this fails, who cares, the look and feel will be Java's
// just continue
}

// create frame and make sure that when you close the frame the
// program exits!
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create your panel, set it to the frame's content pane,
// then show the frame
final JPanel panel = new drumKit();
frame.setContentPane(panel);
frame.setVisible(true);

// resize the frame to be the preferred size of your panel
frame.pack();
}

如果您无法读取这些文件,那么我建议输入完全限定的路径名​​,而不仅仅是 "Snare.wav" (例如),它会在当前 CLASSPATH 目录中查找(对于eclipse,我相信是项目目录)。

关于Java 小程序 : Basic Drum Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23575830/

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