我正在创建一个MP3播放器。它要求用户输入歌曲名称等。但是我一直收到错误说:
PlaylistDriver.java:108: error: cannot find symbol
FileInputStream file = new FileInputStream("C:\\Users\\KThi\\Music\\" + name + ".mp3");
symbol: variable name
location: class PlaylistDriver.playMusicListener
1 error
变量“name”已在以下声明:
public static void addSong()
{//Add Song
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter song name: ");
String name = keyboard.nextLine(); <----------- Variable "name" declared
这是完整的代码:
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class PlaylistDriver
{//Start of class
public static Playlist list;
private AdvancedPlayer player;
public static void main(String[] args) throws Exception
{//Start of main
list = new Playlist();
new PlaylistDriver().SETUP();
while(true)
{
menu();
processInput();
}
}
public void SETUP()
{
JFrame frame = new JFrame("MP3 PLAYER");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnplay = new JButton("Play");
JButton btnstop = new JButton("Stop");
btnplay.addActionListener(new playMusicListener());
btnstop.addActionListener(new stopMusicListener());
frame.getContentPane().add(BorderLayout.WEST, btnplay);
frame.getContentPane().add(BorderLayout.EAST, btnstop);
frame.setVisible(true);
}
public static void processInput()
{//Start of Process Input
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine().toLowerCase();
switch(input.charAt(0))
{//Start of switch
case 'a':
addSong();
break;
case 'i':
printSongByIndex();
break;
case 'n':
removeSongByName();
break;
case 'p':
printSongs();
break;
case 's':
getSize();
break;
case 't':
getTotalTime();
break;
case 'f':
getFormattedTime();
break;
case 'c':
clearSongs();
break;
case 'q':
System.exit(0);
break;
default:
System.out.println("...........");
}//End of switch
}//End of Process Input
public static void addSong()
{//Add Song
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter song name: ");
String name = keyboard.nextLine();
System.out.print("Please enter song artist: ");
String artist = keyboard.nextLine();
System.out.print("Please enter an album: ");
String album = keyboard.nextLine();
System.out.print("Please enter genre: ");
String genre = keyboard.nextLine();
System.out.print("Please enter length of song: ");
double length = keyboard.nextDouble();
Song information = new Song(name, artist, album, length, genre);
list.add(information);
}//End of Add Song
public class playMusicListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
try
{
FileInputStream file = new FileInputStream("C:\\Users\\KThi\\Music\\" + name + ".mp3");
player = new AdvancedPlayer(file);
player.play();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public class stopMusicListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
try
{
FileInputStream file = new FileInputStream("");
player = new AdvancedPlayer(file);
player.stop();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static void printSongs()
{//Print Song Information
System.out.println(list);
}//End of Print Song Information
public static void printSongByIndex()
{//Print all songs
System.out.println("Please enter index of song to view: ");
Scanner keyboard = new Scanner(System.in);
int information = keyboard.nextInt();
System.out.print(list.get(information));
}
public static void removeSongByName()
{//To remove song
System.out.print("\nPlease enter the name of the song to remove: ");
Scanner keyboard = new Scanner(System.in);
String name = keyboard.nextLine();
list.remove( name );
}//End of remove song
public static void getSize()
{//Get size of song
System.out.println("\nTotal number of songs: " + list.size());
}
public static void getTotalTime()
{//Get Total Time
System.out.println("Total Time: " + list.totalTime());
}
public static void getFormattedTime()
{//Get formatted time
System.out.println("Total Formatted Time:" + list.formattedTotalTime());
}
public static void clearSongs()
{//Clear Songs
list.clear();
}
public static void menu()
{//Print Menu
System.out.println("--------MP3 PLAYLIST--------");
System.out.println("[A]dd a Song");
System.out.println("[P]rint songs");
System.out.println("Pr[i]nt song by index");
System.out.println("Remove song by [n]ame");
System.out.println("Get total [s]ize");
System.out.println("Get total [t]ime");
System.out.println("Get [f]ormatted time");
System.out.println("[C]lear all songs");
System.out.println("[Q]uit");
}
}//End of class
答案 0 :(得分:0)
变量名称在类 PlaylistDriver 中定义,但不在 playMusicListener 中定义。
这里的错误原因是你没有在playMusicListener中定义变量名,这反过来会导致错误
btnplay.addActionListener(new playMusicListener());.
因此,根据您的要求,您需要定义一种方法来指定按下播放按钮时要播放的歌曲。