我正在为我的AP ComSci课程创建一个算命先生程序,但我无法按照说明进行ping.wav。
说明:编辑算命先生.java在按下"下一步"按钮时随机回馈一笔财富.wav播放ping。添加 EasyClasses.jar 作为必需的库。
这是我到目前为止的代码:
// 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);
}
}
代码返回了带有ping的财富.wav注释掉了,但是当我尝试取消注释时,我收到一个错误,说"EasySound无法解析为类型"。我有其他使用 EasySound 的代码,但没有收到此错误。我已经将EasyClasses.jar与ping.wav一起添加到项目文件夹中。如何解决这个问题?
我已经将EasyClasses.jar与ping一起添加到项目文件夹中.wav
仅将 jar 添加到项目文件夹是不够的。您还必须将其导入到您的类中。
导入import javax.swing.JButton; import java.awt.Color;
后,您必须导入EasyClasses
.
例如:import com.a.b.EasyClasses;
这里的 com、a 和 b 是包名称,您将EasyClasses.java
导入到项目中,该项目位于jar
文件中的这些包内。