Implement JavaFX music player features
This commit is contained in:
@@ -5,4 +5,6 @@ import javafx.application.Application;
|
||||
public class Launcher{
|
||||
public static void main(String[] args) {
|
||||
// TODO: Launch the JavaFX application by calling Application.launch(Main.class) }
|
||||
}
|
||||
Application.launch(MusicApp.class, args);
|
||||
|
||||
}}
|
||||
@@ -4,6 +4,7 @@ import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -17,6 +18,9 @@ public class MusicApp extends Application {
|
||||
stage.setTitle("Music Player");
|
||||
stage.setScene(scene);
|
||||
//TODO: add icon to the stage
|
||||
stage.getIcons().add(
|
||||
new Image(MusicApp.class.getResourceAsStream("icon.png"))
|
||||
);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,29 +3,85 @@ package sbu.javafx;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.control.ListView;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class MusicController {
|
||||
|
||||
// TODO: Define your UI components using the @FXML annotation.
|
||||
@FXML
|
||||
private Label nowPlayingLabel;
|
||||
|
||||
private final ArrayList<String> playlist = new ArrayList<>();
|
||||
|
||||
|
||||
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
|
||||
|
||||
@FXML
|
||||
public void handlePlayAction() {
|
||||
public void handlePlayAction(ActionEvent event) {
|
||||
//Update labels, change button icons, etc.
|
||||
}
|
||||
Button button = (Button) event.getSource();
|
||||
String songName = button.getUserData().toString();
|
||||
|
||||
nowPlayingLabel.setText("Now Playing : " + songName);
|
||||
|
||||
|
||||
}
|
||||
|
||||
//TODO: Logic to add a specific song to the user's playlist.
|
||||
|
||||
public void addToPlaylist(String songName) {
|
||||
//Add the song to a list or update a ListView.
|
||||
if (!playlist.contains(songName)) {
|
||||
playlist.add(songName);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void addSong1() {
|
||||
addToPlaylist("Shape Of You");
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void addSong2() {
|
||||
addToPlaylist("Believer");
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void addSong3() {
|
||||
addToPlaylist("Perfect");
|
||||
}
|
||||
|
||||
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
|
||||
|
||||
@FXML
|
||||
public void openPlaylistWindow() {
|
||||
//Create a new stage and show the playlist UI.
|
||||
Stage stage = new Stage();
|
||||
|
||||
VBox root = new VBox(15);
|
||||
root.setPadding(new Insets(20));
|
||||
|
||||
Label title = new Label("My Playlist");
|
||||
|
||||
ListView<String> listView = new ListView<>();
|
||||
listView.getItems().addAll(playlist);
|
||||
|
||||
Button closeButton = new Button("Close");
|
||||
closeButton.setOnAction(e -> stage.close());
|
||||
|
||||
root.getChildren().addAll(title, listView, closeButton);
|
||||
|
||||
Scene scene = new Scene(root, 350, 400);
|
||||
|
||||
stage.setTitle("Playlist");
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user