大神请帮忙解决这个java问题,大作业谢谢!!!
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.applet.AudioCl...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.*;
public class Music extends JFrame implements ActionListener,ItemListener{
boolean looping=false;
String[] choics={"ww.wav","夜空中最亮的星.wav"};
String random="";
URL file1=getClass().getResource(choics[0]);
URL file2=getClass().getResource(choics[1]);
AudioClip sound1=java.applet.Applet.newAudioClip(file1);
AudioClip sound2=java.applet.Applet.newAudioClip(file2);
AudioClip chosenClip=sound1;
JComboBox jcbFiles=new JComboBox(choics);
JButton playButton=new JButton("播放");
JButton stopButton=new JButton("停止");
JButton randomButton=new JButton("随机");
JLabel status=new JLabel("选择播放文件");
JPanel controPanel=new JPanel();
Container container=getContentPane();
public Music(){
super("音频播放程序");
jcbFiles.setSelectedIndex(0);
jcbFiles.addItemListener(this);
playButton.addActionListener(this);
stopButton.addActionListener(this);
randomButton.addActionListener(this);
stopButton.setEnabled(false);
controPanel.add(playButton);
controPanel.add(stopButton);
controPanel.add(randomButton);
container.add(jcbFiles,BorderLayout.NORTH);
container.add(controPanel,BorderLayout.CENTER);
container.add(status,BorderLayout.SOUTH);
setSize(300,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e){
if(jcbFiles.getSelectedIndex()==0){
chosenClip=sound1;
}else{
chosenClip=sound2;
}
}
public void actionPerformed(ActionEvent event){
if(chosenClip==null){
status.setText("音频未载入");
return;
}
Object source=event.getSource();
if(source==playButton){
stopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}
if(source==randomButton){
stopButton.setEnabled(true);
int index=(int)(Math.random()*choics.length);
chosenClip.play(choics[index]);
}
if(source==stopButton){
if(looping){
looping=false;
chosenClip.stop();
}else{
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
public static void main(String s[]){
new Music();
}
}
想实现随机播放音乐功能,但是运行出现
--------------------Configuration: <Default>--------------------
D:\javaExamples\music\Music.java:61: 无法将 java.applet.AudioClip 中的 play() 应用于 (java.lang.String)
chosenClip.play(choics[index]);
不知道怎么解决 展开
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.*;
public class Music extends JFrame implements ActionListener,ItemListener{
boolean looping=false;
String[] choics={"ww.wav","夜空中最亮的星.wav"};
String random="";
URL file1=getClass().getResource(choics[0]);
URL file2=getClass().getResource(choics[1]);
AudioClip sound1=java.applet.Applet.newAudioClip(file1);
AudioClip sound2=java.applet.Applet.newAudioClip(file2);
AudioClip chosenClip=sound1;
JComboBox jcbFiles=new JComboBox(choics);
JButton playButton=new JButton("播放");
JButton stopButton=new JButton("停止");
JButton randomButton=new JButton("随机");
JLabel status=new JLabel("选择播放文件");
JPanel controPanel=new JPanel();
Container container=getContentPane();
public Music(){
super("音频播放程序");
jcbFiles.setSelectedIndex(0);
jcbFiles.addItemListener(this);
playButton.addActionListener(this);
stopButton.addActionListener(this);
randomButton.addActionListener(this);
stopButton.setEnabled(false);
controPanel.add(playButton);
controPanel.add(stopButton);
controPanel.add(randomButton);
container.add(jcbFiles,BorderLayout.NORTH);
container.add(controPanel,BorderLayout.CENTER);
container.add(status,BorderLayout.SOUTH);
setSize(300,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e){
if(jcbFiles.getSelectedIndex()==0){
chosenClip=sound1;
}else{
chosenClip=sound2;
}
}
public void actionPerformed(ActionEvent event){
if(chosenClip==null){
status.setText("音频未载入");
return;
}
Object source=event.getSource();
if(source==playButton){
stopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}
if(source==randomButton){
stopButton.setEnabled(true);
int index=(int)(Math.random()*choics.length);
chosenClip.play(choics[index]);
}
if(source==stopButton){
if(looping){
looping=false;
chosenClip.stop();
}else{
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
public static void main(String s[]){
new Music();
}
}
想实现随机播放音乐功能,但是运行出现
--------------------Configuration: <Default>--------------------
D:\javaExamples\music\Music.java:61: 无法将 java.applet.AudioClip 中的 play() 应用于 (java.lang.String)
chosenClip.play(choics[index]);
不知道怎么解决 展开
2个回答
展开全部
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Music extends JFrame implements ActionListener, ItemListener {
boolean looping = false;
String[] choics = { "aa.wav", "aa.wav" };
String random = "";
URL file1;
URL file2;
AudioClip sound1;
AudioClip sound2;
AudioClip chosenClip;
JComboBox jcbFiles = new JComboBox(choics);
JButton playButton = new JButton("播放");
JButton stopButton = new JButton("停止");
JButton randomButton = new JButton("随机");
JLabel status = new JLabel("选择播放文件");
JPanel controPanel = new JPanel();
Container container = getContentPane();
public Music() {
super("音频播放程序");
file1 = getClass().getClassLoader().getResource(choics[0]);
// file2 = getClass().getClassLoader().getResource(choics[1]);
file2 = getClass().getResource(choics[1]);
System.out.println(file1);
System.out.println(file2);
sound1 = java.applet.Applet.newAudioClip(file1);
sound2 = java.applet.Applet.newAudioClip(file2);
chosenClip = sound1;
jcbFiles.setSelectedIndex(0);
jcbFiles.addItemListener(this);
playButton.addActionListener(this);
stopButton.addActionListener(this);
randomButton.addActionListener(this);
stopButton.setEnabled(false);
controPanel.add(playButton);
controPanel.add(stopButton);
controPanel.add(randomButton);
container.add(jcbFiles, BorderLayout.NORTH);
container.add(controPanel, BorderLayout.CENTER);
container.add(status, BorderLayout.SOUTH);
setSize(300, 130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e) {
if (jcbFiles.getSelectedIndex() == 0) {
chosenClip = sound1;
} else {
chosenClip = sound2;
}
}
public void actionPerformed(ActionEvent event) {
if (chosenClip == null) {
status.setText("音频未载入");
return;
}
Object source = event.getSource();
if (source == playButton) {
stopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}
if (source == randomButton) {
stopButton.setEnabled(true);
int index = (int) (Math.random() * choics.length);
chosenClip.play();
// chosenClip.play(choics[index]);
}
if (source == stopButton) {
if (looping) {
looping = false;
chosenClip.stop();
} else {
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
public static void main(String s[]) {
new Music();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询