javafx projekt #1

Merged
MehrdadShirvani merged 1 commits from develop into main 2026-07-10 17:15:19 +00:00
8 changed files with 243 additions and 16 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
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+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, args);
}
}
+4 -4
View File
@@ -3,20 +3,20 @@ 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;
import java.util.Objects;
public class MusicApp extends Application {
@Override
public void start(Stage stage) throws IOException
{
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
Scene scene = new Scene(fxmlLoader.load(), 800, 500);
stage.setTitle("Music Player");
stage.setScene(scene);
//TODO: add icon to the stage
stage.show();
}
+124 -8
View File
@@ -1,31 +1,147 @@
package sbu.javafx;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
public class MusicController {
@FXML
private Button themeButton;
// TODO: Define your UI components using the @FXML annotation.
@FXML
private Label nowPlayingLabel;
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
@FXML
private Button playButton1,playButton2,playButton3;
@FXML
private HBox row1, row2, row3;
@FXML
private VBox mainContainer;
private ArrayList <String> playList = new ArrayList<>();
private String currentSong = "";
private boolean isDarkMode = false;
@FXML
public void toggleTheme(){
isDarkMode = !isDarkMode;
if(isDarkMode){
mainContainer.setStyle("-fx-padding: 20; -fx-background-color: #1e1e1e;");
nowPlayingLabel.setStyle("-fx-padding: 15; -fx-font-style: italic; -fx-background-color: #333; -fx-text-fill: white; -fx-background-radius: 5;");
themeButton.setText("Light Mode");
themeButton.setStyle("-fx-background-color: #f39c12; -fx-text-fill: white; -fx-padding: 5 10; -fx-background-radius: 5;");
}else{
mainContainer.setStyle("-fx-padding: 20; -fx-background-color: #f0f0f0;");
nowPlayingLabel.setStyle("-fx-padding: 15; -fx-font-style: italic; -fx-background-color: #e0e0e0; -fx-text-fill: black; -fx-background-radius: 5;");
themeButton.setText("Dark Mode");
themeButton.setStyle("-fx-background-color: #34495e; -fx-text-fill: white; -fx-padding: 5 10;");
}
}
private void resetRowHighlight() {
row1.setStyle("-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;");
row2.setStyle("-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;");
row3.setStyle("-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;");
}
private void resetAllPlayButtons(){
playButton1.setText("▶️Play");
playButton1.setStyle("-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 5;");
playButton2.setText("▶️Play");
playButton2.setStyle("-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 5;");
playButton3.setText("▶️Play");
playButton3.setStyle("-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 5;");
}
@FXML
public void playSong1() {
resetAllPlayButtons();
resetRowHighlight();
currentSong = "Song of Dawn - Alice Johnson";
nowPlayingLabel.setText("Now Playing: "+ currentSong);
playButton1.setText("⏸️Playing");
playButton1.setStyle("-fx-background-color: #f39c12; -fx-text-fill: white; -fx-background-radius: 5;");
}
@FXML
public void playSong2() {
resetAllPlayButtons();
resetRowHighlight();
currentSong = "Midnight Drive - The Night Owls";
nowPlayingLabel.setText("Now Playing: "+ currentSong);
playButton2.setText("⏸️Playing");
playButton2.setStyle("-fx-background-color: #f39c12; -fx-text-fill: white; -fx-background-radius: 5;");
}
@FXML
public void playSong3() {
resetAllPlayButtons();
resetRowHighlight();
currentSong = "Echoes of You - Luna Sky";
nowPlayingLabel.setText("Now Playing: "+ currentSong);
playButton3.setText("⏸️Playing");
playButton3.setStyle("-fx-background-color: #f39c12; -fx-text-fill: white; -fx-background-radius: 5;");
}
@FXML
public void handlePlayAction() {
//Update labels, change button icons, etc.
}
//TODO: Logic to add a specific song to the user's playlist.
@FXML
public void addSong1ToPlaylist() {
playList.add("Song of Dawn - Alice Johnson (03:45)");
System.out.println("Added: Song of Dawn");
}
@FXML
public void addSong2ToPlaylist() {
playList.add("Midnight Drive - The Night Owls (04:12)");
System.out.println("Added: Midnight Drive");
}
@FXML
public void addSong3ToPlaylist() {
playList.add("Echoes of You - Luna Sky (05:08)");
System.out.println("Added: Echoes of You");
}
public void addToPlaylist(String songName) {
//Add the song to a list or update a ListView.
}
// 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.
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist-view.fxml"));
VBox root = loader.load();
PlaylistController playlistController = loader.getController();
playlistController.setPlaylist(playList);
Scene scene = new Scene(root, 450 , 400);
Stage playlistStage = new Stage();
playlistStage.setTitle("Playlist");
playlistStage.setScene(scene);
playlistStage.show();
} catch (IOException e) {
e.getMessage();
}
}
}
@@ -0,0 +1,36 @@
package sbu.javafx;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
public class PlaylistController {
@FXML
private VBox playlistContainer;
public void setPlaylist(ArrayList<String> playlist){
playlistContainer.getChildren().clear();
if (playlist.isEmpty()){
Label emptyLabel = new Label("Playlist is empty. Add some songs!");
emptyLabel.setStyle("-fx-text-fill: #999; -fx-font-style: italic;");
playlistContainer.getChildren().add(emptyLabel);
}else {
for (int i = 0; i < playlist.size(); i++) {
Label songLabel = new Label((i+1) + ". " + playlist.get(i));
songLabel.setStyle("-fx-padding: 5; -fx-font-size: 14px;");
playlistContainer.getChildren().add(songLabel);
}
}
}
@FXML
public void closePlaylistWindow(ActionEvent actionEvent) {
Stage stage = (Stage) playlistContainer.getScene().getWindow();
stage.close();
}
}
+49 -2
View File
@@ -1,8 +1,55 @@
<?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.layout.HBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<!-- TODO: Add your code here! -->
<VBox fx:id="mainContainer" xmlns="http://javafx.com/fxml"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sbu.javafx.MusicController"
spacing="10"
alignment="TOP_CENTER"
style="-fx-padding: 20; -fx-background-color: #f0f0f0;">
<Label text="Music Player" style="-fx-font-size: 24px; -fx-font-weight: bold;"/>
<Button fx:id="themeButton" text="Dark Mode" onAction="#toggleTheme" style="-fx-background-color: #34495e; -fx-text-fill: white; -fx-padding: 5 10;" />
<HBox spacing="40" style="-fx-padding: 10 0 5 0;">
<Label text="Song Title" minWidth="100" style="-fx-font-weight: bold;" />
<Label text="Artist Name" minWidth="100" style="-fx-font-weight: bold;" />
<Label text="Duration" minWidth="60" style="-fx-font-weight: bold;" />
<Label text="" minWidth="100" />
<Label text="" minWidth="50" />
</HBox>
<HBox fx:id="row1" spacing="20" alignment="CENTER_LEFT" style="-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;">
<Label text="Song of Dawn" minWidth="100" />
<Label text="Alice Johnson" minWidth="100" />
<Label text="03:45" minWidth="60" />
<Button text="Add to Playlist" onAction="#addSong1ToPlaylist" />
<Button fx:id="playButton1" text="play" onAction="#playSong1"/>
</HBox>
<HBox fx:id="row2" spacing="20" alignment="CENTER_LEFT" style="-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;">
<Label text="Midnight Drive" minWidth="100" />
<Label text="The Night Owls" minWidth="100" />
<Label text="04:12" minWidth="60" />
<Button text="Add to Playlist" onAction="#addSong2ToPlaylist" />
<Button fx:id="playButton2" text="play" onAction="#playSong2"/>
</HBox>
<HBox fx:id="row3" spacing="20" alignment="CENTER_LEFT" style="-fx-background-color: white; -fx-padding: 12; -fx-background-radius: 8;">
<Label text="Echoes of You" minWidth="100" />
<Label text="Luna Sky" minWidth="100" />
<Label text="05:08" minWidth="60" />
<Button text="Add to Playlist" onAction="#addSong3ToPlaylist" />
<Button fx:id="playButton3" text="play" onAction="#playSong3"/>
</HBox>
<Label fx:id="nowPlayingLabel" text="Now Playing: -" style="-fx-padding: 15; -fx-font-style: italic; -fx-background-color: #e0e0e0; -fx-background-radius: 5;" />
<Button text="Open Playlist" onAction="#openPlaylistWindow" style="-fx-background-color: #3498db; -fx-text-fill: white; -fx-padding: 8 15;" />
</VBox>
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sbu.javafx.PlaylistController"
spacing="15"
alignment="TOP_CENTER"
style="-fx-padding: 20; -fx-background-color: #f5f5f5;">
<Label text="My Playlist" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #2c3e50;" />
<VBox fx:id="playlistContainer" spacing="8" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #2c3e50;"/>
<Button text="Close" onAction="#closePlaylistWindow" style="-fx-background-color: #e74c3c; -fx-text-fill: white; -fx-padding: 8 20;"/>
</VBox>