1 Commits
Author SHA1 Message Date
faraz_ardeh 47fbbac7e8 introduction to JavaFX 2026-07-12 18:39:21 +03:30
4 changed files with 54 additions and 16 deletions
+41 -9
View File
@@ -1,41 +1,73 @@
package workshop; package workshop;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label; import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class Controller { public class Controller {
@FXML private Label titleLabel; @FXML private Label titleLabel;
@FXML private Button themeToggle; @FXML private Button themeToggle;
@FXML private Label basicPriceLabel;
@FXML private Label proPriceLabel;
@FXML private Label enterprisePriceLabel;
private boolean isDark = true; private boolean isDark = true;
// TODO: (Optional:For Practice) Create initialize function and use it to set prices during runtime. private double basicPrice = 9.99;
private double proPrice = 19.99;
private int enterprisePrice = 0; // contact us, no numeric price
@FXML
public void initialize() {
basicPriceLabel.setText(String.format("$%.2f / month", basicPrice));
proPriceLabel.setText(String.format("$%.2f / month", proPrice));
enterprisePriceLabel.setText("Contact us");
}
// TODO: (Optional:For Practice) Make the plan price go 1 dollar up each time user clicks on the shop button.
@FXML @FXML
private void buyBasic() { private void buyBasic() {
// TODO: Call showAlert() with a message indicating the user entered the payment gateway basicPrice += 1;
basicPriceLabel.setText(String.format("$%.2f / month", basicPrice));
showAlert("You've entered the payment gateway for the Basic plan!");
} }
@FXML @FXML
private void buyPro() { private void buyPro() {
// TODO: Call showAlert() with a message indicating the user entered the payment gateway proPrice += 1;
proPriceLabel.setText(String.format("$%.2f / month", proPrice));
showAlert("You've entered the payment gateway for the Pro plan!");
} }
@FXML @FXML
private void buyEnterprise() { private void buyEnterprise() {
// TODO: Call showAlert() with a message indicating the user entered the payment gateway showAlert("You've entered the payment gateway for the Enterprise plan!");
} }
@FXML @FXML
private void toggleTheme() { private void toggleTheme() {
// TODO: Toggle the isDark flag. If dark, switch to style.css and set button text to "Switch to Light Mode" if (isDark) {
// TODO: If light, switch to light.css and set button text to "Switch to Dark Mode" titleLabel.getScene().getStylesheets().clear();
titleLabel.getScene().getStylesheets().add(
getClass().getResource("light.css").toExternalForm()
);
themeToggle.setText("Switch to Dark Mode");
} else {
titleLabel.getScene().getStylesheets().clear();
titleLabel.getScene().getStylesheets().add(
getClass().getResource("style.css").toExternalForm()
);
themeToggle.setText("Switch to Light Mode");
}
isDark = !isDark;
} }
private void showAlert(String message) { private void showAlert(String message) {
// TODO: Create a new Alert of type INFORMATION with the given message and display it Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Payment Gateway");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
} }
} }
+1 -1
View File
@@ -4,6 +4,6 @@ import javafx.application.Application;
public class Launcher { public class Launcher {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Launch the JavaFX application by calling Application.launch(Main.class) Application.launch(Main.class, args);
} }
} }
+9 -3
View File
@@ -1,13 +1,19 @@
package workshop; package workshop;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
@Override @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) throws Exception {
// TODO: Load the FXML file using FXMLLoader.load() and get the Parent root Parent root = FXMLLoader.load(getClass().getResource("pricing.fxml"));
// TODO: Create a Scene with the root (width=750, height=450) and add the stylesheet "style.css" Scene scene = new Scene(root, 750, 450);
// TODO: Set the stage title to "Pricing Plans", set the scene, and call stage.show() scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
stage.setTitle("Pricing Plans");
stage.setScene(scene);
stage.show();
} }
} }
+3 -3
View File
@@ -17,21 +17,21 @@
<HBox spacing="25" alignment="CENTER" HBox.hgrow="ALWAYS"> <HBox spacing="25" alignment="CENTER" HBox.hgrow="ALWAYS">
<VBox styleClass="card" prefWidth="200"> <VBox styleClass="card" prefWidth="200">
<Label text="Basic" styleClass="plan-name" /> <Label text="Basic" styleClass="plan-name" />
<Label text="\$9.99 / month" styleClass="price" /> <Label fx:id="basicPriceLabel" text="\$9.99 / month" styleClass="price" />
<Label text="Single user, basic features" styleClass="desc" wrapText="true" /> <Label text="Single user, basic features" styleClass="desc" wrapText="true" />
<Button text="Shop" onAction="#buyBasic" styleClass="shop-btn" /> <Button text="Shop" onAction="#buyBasic" styleClass="shop-btn" />
</VBox> </VBox>
<VBox styleClass="card" prefWidth="200"> <VBox styleClass="card" prefWidth="200">
<Label text="Pro" styleClass="plan-name" /> <Label text="Pro" styleClass="plan-name" />
<Label text="\$19.99 / month" styleClass="price" /> <Label fx:id="proPriceLabel" text="\$19.99 / month" styleClass="price" />
<Label text="Up to 5 users, advanced features" styleClass="desc" wrapText="true" /> <Label text="Up to 5 users, advanced features" styleClass="desc" wrapText="true" />
<Button text="Shop" onAction="#buyPro" styleClass="shop-btn" /> <Button text="Shop" onAction="#buyPro" styleClass="shop-btn" />
</VBox> </VBox>
<VBox styleClass="card" prefWidth="200"> <VBox styleClass="card" prefWidth="200">
<Label text="Enterprise" styleClass="plan-name" /> <Label text="Enterprise" styleClass="plan-name" />
<Label text="Contact us" styleClass="price" /> <Label fx:id="enterprisePriceLabel" text="Contact us" styleClass="price" />
<Label text="Unlimited users, custom solutions" styleClass="desc" wrapText="true" /> <Label text="Unlimited users, custom solutions" styleClass="desc" wrapText="true" />
<Button text="Shop" onAction="#buyEnterprise" styleClass="shop-btn" /> <Button text="Shop" onAction="#buyEnterprise" styleClass="shop-btn" />
</VBox> </VBox>