Here is My Main.java File
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
String css= this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Application.launch(args);
}
}
This is my SwitchController.java file
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SceneSwitcher {
private Stage stage;
private Scene scene;
private Parent parent;
public void switchToMain(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("Main.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
public void switchToAnotherApp(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("AnotherApp.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
}
this is my application.css file
.main{
-fx-background-color: "red";
}
#sc1{
-fx-alignment: center;
}
#sc2{
-fx-alignment: center;
-fx-text-fill: "GREEN";
-fx-outline:none;
}
.label{
-fx-font-size: 20;
-fx-font-family: "Lucida Console";
-fx-text-fill: "white";
-fx-alignment: center;
}
#titleLabel{
-fx-font-size: 60;
}
this is AnotherApp.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneSwitcher">
<children>
<Button fx:id="sc1" layoutX="197.0" layoutY="272.0" mnemonicParsing="false" onAction="#switchToMain" prefHeight="84.0" prefWidth="187.0" text="Switch to Main Scene" />
<Label layoutX="1.0" layoutY="50.0" prefHeight="17.0" prefWidth="600.0" text="This is Scene 2" />
</children>
</AnchorPane>
This is my Main.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneSwitcher">
<children>
<Label fx:id="titleLabel" layoutX="1.0" layoutY="143.0" prefHeight="17.0" prefWidth="600.0" text="Level 1" />
<Label fx:id="secondLevelLabel" layoutX="2.0" layoutY="242.0" prefHeight="17.0" prefWidth="600.0" text="this has some CSS styles" />
<Button fx:id="sc2" layoutX="213.0" layoutY="292.0" mnemonicParsing="false" onAction="#switchToAnotherApp" prefHeight="78.0" prefWidth="177.0" text="Switch to Another App" />
</children>
</AnchorPane>
I am just starting to learn JavaFX and Java, inspired by [Bro code] https://www.youtube.com/watch?v=9XJicRt_FaI&ab_channel=BroCode. If you have any tips or suggestions for me, please comment below. Thank you!