I am writing a program, but I cannot find what is wrong with the code.
Only need to show one MP3, or an error message if there are any missing fields or an incorrect entry for seconds. It is not storing information about previous songs; it just display the current MP3.
Here are the problem entire descriptions.
Create a class MP3 with instance variables for: artist, song, album, track length (in seconds). Provide a constructor, get/set methods, and a toString. method. The toString method should convert the track length into minutes and seconds. For example, 265 seconds would be displayed as 4:25.
Create a class MP3Manager that extends JFrame with the following: a JPanel with a GridLayout that contains 4 labels, 4 textfields, and 4 buttons. This will be added to the container’s NORTH position.
The textfields are for user input of an MP3 record: artist, song, album, track length.
One button will be used to add an MP3 to a text file containing a list of MP3 files.
A second button will display all MP3s stored in the file.
The third button will find and display all MP3 files if the user enters the artist’s name and/or or the album name. The delete button will delete a single MP3 from the file based on the song title and artist name.
A JTextArea to display the MP3s. Add it to the container’s CENTER position.
Use a named inner class ButtonHandler to handle the button events. Note: the only event you will enable will be to add an MP3 (button 1). Just capture the input data in the 4 fields, create an instance of the MP3 class, and call the toString method to display it in the text area. If data is missing from one of the fields, inform the user can be informed by the TextArea.
Create an application MP3ManagerTest.
Here is my MP3 class:
public class MP3{
private String artist;
private String song;
private String album;
private int trackLength;
public MP3(String artistName, String songName, String albumName, int trackLeng){
setArtist(artistName);
setSong(songName);
setAlbum(albumName);
setTrackLength(trackLeng);
}
public void setArtist(String artistName){
artist = artistName;
}
public String getArtist(){
return artist;
}
public void setSong(String songName){
song = songName;
}
public String getSong(){
return song;
}
public void setAlbum(String albumName){
album = albumName;
}
public String getAlbum(){
return album;
}
public void setTrackLength(int trackLeng){
trackLength = trackLeng;
}
public int getTrackLength(){
return trackLength;
}
public String toString(){
return String.format("%s, %s, %s, %d : %d", getArtist(),
getSong(), getAlbum(), getTrackLength() / 60,
getTrackLength() - (getTrackLength() / 60) * 60);
}
}
And here is the MP3Manager class,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MP3Manager extends JFrame{
private JButton addMP3Button, displayMP3sButton, findMP3Button, deleteMP3Button;
private JLabel artistLabel, songLabel, albumLabel, trackLengthLabel;
private JTextField artistField, songField, albumField, trackLengthField;
private JPanel panel, buttonPanel, fieldPanelArtist, fieldPanelSong, fieldPanelAlbum, fieldPanelTrackLength;
public MP3Manager(){
super("MP3 Manager");
artistLabel = new JLabel("Artist Name ");
artistField = new JTextField();
fieldPanelArtist = new JPanel();
fieldPanelArtist.setLayout(new FlowLayout());
fieldPanelArtist.add(artistLabel);
fieldPanelArtist.add(artistField);
songLabel = new JLabel("Song Title ");
songField = new JTextField();
fieldPanelSong = new JPanel();
fieldPanelSong.setLayout(new FlowLayout());
fieldPanelSong.add(songLabel);
fieldPanelSong.add(songField);
albumLabel = new JLabel("Album Name ");
albumField = new JTextField();
fieldPanelAlbum = new JPanel();
fieldPanelAlbum.setLayout(new FlowLayout());
fieldPanelAlbum.add(albumLabel);
fieldPanelAlbum.add(albumField);
trackLengthLabel = new JLabel("Track Length (in seconds) ");
trackLengthField = new JTextField("325", 16);
fieldPanelTrackLength = new JPanel();
fieldPanelTrackLength.setLayout(new FlowLayout());
fieldPanelTrackLength.add(trackLengthLabel);
fieldPanelTrackLength.add(trackLengthField);
addMP3Button = new JButton(" Add MP3 ");
displayMP3sButton = new JButton(" Display MP3s ");
findMP3Button = new JButton(" Find MP3 ");
deleteMP3Button = new JButton(" Delete MP3 ");
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2, 2, 5, 5));
buttonPanel.add(addMP3Button);
buttonPanel.add(displayMP3sButton);
buttonPanel.add(findMP3Button);
buttonPanel.add(deleteMP3Button);
Container container = getContentPane();
container.setLayout(new GridLayout(5, 1, 5, 5));
container.add(fieldPanelArtist);
container.add(fieldPanelSong);
container.add(fieldPanelAlbum);
container.add(fieldPanelTrackLength);
container.add(buttonPanel);
panel = new JPanel();
panel.add(container, BorderLayout.NORTH );
JTextArea textArea = new JTextArea();
panel.add(textArea, BorderLayout.CENTER);
ButtonHandler handler = new ButtonHandler();
addMP3Button.addActionListener(handler);
displayMP3sButton.addActionListener(handler);
findMP3Button.addActionListener(handler);
deleteMP3Button.addActionListener(handler);
}
private class ButtonHandler implements ActionListener{
int length = Integer.parseInt(trackLengthField.getText());
String artist = artistField.getText();
String album = albumField.getText();
String song = songField.getText();
MP3 list = new MP3(artist, song, album, length);
String str = "";
public void actionPerformed(ActionEvent event){
if(event.getSource() == addMP3Button){
if(artist == "" || album == "" || song == "" || trackLengthField.getText() == "")
str = "Please fill in complete information";
else if(length <= 0)
str = "Please fill in integer for track length";
else
str = list.toString();
}
// else if(event.getSource() == displayMP3sButton)
// else if(event.getSource() == findMP3Button)
// else(event.getSource() == deleteMP3Button)
TextArea textArea = new TextArea(str);
}
}
}
And here is the MP3ManagerTest
import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
//mian
public static void main(String [] args){
MP3Manager manager = new MP3Manager();
manager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
manager.setSize(650,650);
manager.setVisible(true);
}
}
After I run MP3ManagerTest, it has a window, but nothing on it.