Implement JavaFX music player features

This commit is contained in:
Fatemesadat Mirabootalebi
2026-07-24 10:23:17 -07:00
parent fa5f8ab267
commit caa854b565
8 changed files with 148 additions and 7 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+3 -1
View File
@@ -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
View File
@@ -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();
}
+58 -2
View File
@@ -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();
}
}
+75 -2
View File
@@ -1,8 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10">
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<VBox xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sbu.javafx.MusicController"
alignment="TOP_CENTER"
spacing="15"
prefWidth="400"
prefHeight="500">
<!-- TODO: Add your code here! -->
<padding>
<Insets top="20" right="20" bottom="20" left="20"/>
</padding>
<Label text="Music Player"
style="-fx-font-size:24px; -fx-font-weight:bold;"/>
<Label fx:id="nowPlayingLabel"
text="Now Playing : None"
style="-fx-font-size:16px;"/>
<!-- Song 1 -->
<HBox spacing="10" alignment="CENTER_LEFT">
<VBox spacing="3">
<Label text="Shape Of You"/>
<Label text="Ed Sheeran"/>
<Label text="3:55"/>
</VBox>
<Button text="Play"
onAction="#handlePlayAction"
userData="Shape Of You"/>
<Button text="Add to Playlist"
onAction="#addSong1"/>
</HBox>
<!-- Song 2 -->
<HBox spacing="10" alignment="CENTER_LEFT">
<VBox spacing="3">
<Label text="Believer"/>
<Label text="Imagine Dragons"/>
<Label text="3:24"/>
</VBox>
<Button text="Play"
onAction="#handlePlayAction"
userData="Believer"/>
<Button text="Add to Playlist"
onAction="#addSong2"/>
</HBox>
<!-- Song 3 -->
<HBox spacing="10" alignment="CENTER_LEFT">
<VBox spacing="3">
<Label text="Perfect"/>
<Label text="Ed Sheeran"/>
<Label text="4:21"/>
</VBox>
<Button text="Play"
onAction="#handlePlayAction"
userData="Perfect"/>
<Button text="Add to Playlist"
onAction="#addSong3"/>
</HBox>
<Button text="View Playlist"
onAction="#openPlaylistWindow"/>
</VBox>
Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
View File
Binary file not shown.