Java 如何让EasySound(一个.wav播放器)在我的代码中工作?
我正在为AP ComSci课程创建一个算命程序,但我无法让ping.wav按照说明工作 说明:编辑fortuteller.java以在按下“下一步”按钮时随机返回fortune和play ping.wav。将EasyClasses.jar添加为所需库 以下是我目前的代码:Java 如何让EasySound(一个.wav播放器)在我的代码中工作?,java,audio,Java,Audio,我正在为AP ComSci课程创建一个算命程序,但我无法让ping.wav按照说明工作 说明:编辑fortuteller.java以在按下“下一步”按钮时随机返回fortune和play ping.wav。将EasyClasses.jar添加为所需库 以下是我目前的代码: // Fortune Teller import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.
// Fortune Teller
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
public class FortuneTeller extends JFrame implements ActionListener{
// private static final EasySound ding = new EasySound("ding.wav");
private String[] fortune = {"You will find a penny", "You like cheese", "You will get a car","You will get ebola",
"You will becomea DOTA pro"};
private JTextField display;
public FortuneTeller()
{
super("Fortune Teller");
display = new JTextField(" Press \"Next\" to see your fortune...", 25);
display.setBackground(Color.WHITE);
display.setEditable(false);
JButton go = new JButton("Next");
go.addActionListener(this);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(display);
c.add(go);
}
public void actionPerformed(ActionEvent e)
{
// Pick and display a random fortune:
int r = (int)(Math.random() * 5);
String f = fortune[r];
display.setText(" " + f );
// ding.play();
}
public static void main(String[] args)
{
JFrame window = new FortuneTeller();
window.setBounds(300, 300, 300, 100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
}
}
代码返回一个fortune,ping.wav被注释掉,但是当我尝试取消注释时,我得到一个错误,说“EasySound无法解析为类型”。我有其他代码使用EasySound,但没有得到这个错误。我已经将easyclass.jar与ping.wav一起添加到项目文件夹中。如何解决这个问题
我已经将easyclass.jar与ping.wav一起添加到项目文件夹中
仅仅将jar添加到项目文件夹是不够的。您还必须将其导入到类中
导入import javax.swing.JButton;导入java.awt.Color
您必须导入EasyClasses
例如:导入com.a.b.EasyClasses
这里com、a和b是包名,您正在将easyclass.java
导入到jar
文件中那些包中的项目中