I am writing a Java program, a MP3 Manager, but I have problems to work with buttons, TextFields, and TextArea.
Here are the problem 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.
I started to write these programs. Here is the 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, but I cannot finish it. I really need help to finish this.
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 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();
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);
add(container, BorderLayout.NORTH );
JTextArea textArea = new JTextArea();
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 my MP3ManagerTest.java
import javax.swing.*;
import java.awt.*;
//class
public class MP3ManagerTest{
//mian
public static void main(String [] args){
MP3Manager manager = new MP3Manager();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(manager);
application.setSize(650,850);
application.setVisible(true);
}
}
The program should have been looked like the photo that I posted with. I am a new programmer, who really don't know much about Java. I am really appreciated if anyone could have helped with me. Thank in advance.