Making a music program

This commit is contained in:
2026-06-09 13:58:56 +03:30
parent fa5f8ab267
commit b5959e5329
8 changed files with 202 additions and 7 deletions
+2 -1
View File
@@ -4,5 +4,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);
}
}
+4 -1
View File
@@ -3,6 +3,7 @@ package sbu.javafx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.IOException;
@@ -16,7 +17,9 @@ public class MusicApp extends Application {
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Music Player");
stage.setScene(scene);
//TODO: add icon to the stage
stage.setWidth(900);
stage.setHeight(500);
stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png")));
stage.show();
}
+100 -4
View File
@@ -1,31 +1,127 @@
package sbu.javafx;
//main
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
//event
import javafx.event.ActionEvent;
//UI controller
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
//layout
import javafx.scene.layout.VBox;
//stage and scene
import javafx.stage.Stage;
import javafx.scene.Scene;
import java.util.ArrayList;
import java.io.IOException;
public class MusicController {
// TODO: Define your UI components using the @FXML annotation.
@FXML private Label nowPlayingLabel;
@FXML private VBox playlistContainer;
@FXML private ListView<String> playlistView;
private ArrayList<String> playlist = new ArrayList<>();
private ArrayList<Song> songList = new ArrayList<>();
private Stage primaryStage;
public void setPrimaryStage(Stage stage) {
this.primaryStage = stage;
}
//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.
String songName;
Button clickedButton = (Button) event.getSource();
String buttonText = clickedButton.getText();
if(clickedButton.getId() != null){
switch (clickedButton.getId()){
case "playM1" :
songName = "Song of Dawn";
break;
case "playM2" :
songName = "Midnight Drive";
break;
case "playM3" :
songName = "Echoes of You";
break;
default:
songName = "Unknown Song";
}
}
else
songName = "Selected Song";
nowPlayingLabel.setText("Now playing: " + songName);
}
//TODO: Logic to add a specific song to the user's playlist.
@FXML
public void addToPlaylist(String songName) {
//Add the song to a list or update a ListView.
playlist.add(songName);
nowPlayingLabel.setText(songName + " added to playlist :)");
if (playlistView != null) {
playlistView.getItems().add(songName);
}
}
@FXML
public void handleAddToPlaylist (ActionEvent event){
Button clickButton = (Button) event.getSource();
String songName;
if(clickButton.getId() != null){
switch (clickButton.getId()){
case "addM1" :
songName = "Song of Dawn | Alice Johnson | 03:45";
break;
case "addM2" :
songName = "Midnight Drive | The Night Owls | 04:12";
break;
case "addM3" :
songName = "Echoes of You | Luna Sky | 05:08";
break;
default:
songName = "Unknown Song";
}
}
else
songName = "Selected Song";
addToPlaylist(songName);
}
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
@FXML
public void openPlaylistWindow() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("playlist-view.fxml"));
Scene scene = new Scene(loader.load());
PlaylistController pc = loader.getController();
pc.setPlaylist(playlist);
Stage stage = new Stage();
stage.setScene(scene);
stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png")));
stage.show();
public void openPlaylistWindow() {
//Create a new stage and show the playlist UI.
}
}
@@ -0,0 +1,18 @@
package sbu.javafx;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import java.util.ArrayList;
public class PlaylistController {
private ArrayList<String> items;
@FXML private ListView<String> itemPlaylist;
public void setPlaylist(ArrayList<String> items){
this.items = items;
for(String item : items){
itemPlaylist.getItems().add(item);
}
}
}
+17
View File
@@ -0,0 +1,17 @@
package sbu.javafx;
public class Song {
private String title;
private String artist;
private String duration;
public Song (String title, String artist, String duration){
this.title = title;
this.artist = artist;
this.duration = duration;
}
public String getTitle () { return title;}
public String getArtist () { return artist;}
public String getDuration () { return duration;}
}
+47 -1
View File
@@ -1,8 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10">
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Separator?>
<VBox xmlns="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:controller="sbu.javafx.MusicController" alignment="CENTER"
spacing="10" style="-fx-font-size: 20px;">
<Label text="Music player"/>
<HBox spacing="25" alignment="CENTER" >
<Label text="Song Title" prefWidth="150"/>
<Label text="Artist Name" prefWidth="150"/>
<Label text="Duration" prefWidth="150"/>
<Label text="Add to Playlist" prefWidth="180"/>
<Label text="Play" prefWidth="100"/>
</HBox>
<Separator/>
<HBox spacing="25" alignment="CENTER" >
<Label text="Song of Dawn" prefWidth="150"/>
<Label text="Alice Johnson" prefWidth="150"/>
<Label text="03:45" prefWidth="150"/>
<Button fx:id="addM1" text="Add to Playlist" prefWidth="180" onAction="#handleAddToPlaylist"/>
<Button fx:id="playM1" text="Play" prefWidth="100" onAction="#handlePlayAction"/>
</HBox>
<HBox spacing="25" alignment="CENTER" >
<Label text="Midnight Drive" prefWidth="150"/>
<Label text="The Night Owls" prefWidth="150"/>
<Label text="04:12" prefWidth="150"/>
<Button fx:id="addM2" text="Add to Playlist" prefWidth="180" onAction="#handleAddToPlaylist"/>
<Button fx:id="playM2" text="Play" prefWidth="100" onAction="#handlePlayAction"/>
</HBox>
<HBox spacing="25" alignment="CENTER" >
<Label text="Echoes of You" prefWidth="150"/>
<Label text="Luna Sky" prefWidth="150"/>
<Label text="05:08" prefWidth="150"/>
<Button fx:id="addM3" text="Add to Playlist" prefWidth="180" onAction="#handleAddToPlaylist"/>
<Button fx:id="playM3" text="Play" prefWidth="100" onAction="#handlePlayAction"/>
</HBox>
<Label fx:id="nowPlayingLabel" text="Now Playing"/>
<Button text="Play list" onAction="#openPlaylistWindow"/>
<!-- TODO: Add your code here! -->
</VBox>
Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Label?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sbu.javafx.PlaylistController"
prefHeight="400.0" prefWidth="600.0">
<Label text="Playlist"/>
<ListView fx:id="itemPlaylist"/>
</VBox>