Stage, Scene, and Controls
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Collections;
public class JavaFXDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaFX Basic Structure");
// Create UI components
Button firstBtn = new Button("First Button");
Button secondBtn = new Button("Second Button");
Button thirdBtn = new Button("Third Button");
// Configure layout container
VBox container = new VBox();
container.getChildren().addAll(Collections.singletonList(firstBtn, secondBtn, thirdBtn));
// Set up scene with dimensions
Scene scene = new Scene(container, 800, 600);
primaryStage.setScene(scene);
// Display the application window
primaryStage.show();
}
}
Stage and Window Events
Window Event Handling
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class WindowEventsDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage mainStage) throws Exception {
mainStage.setTitle("JavaFX Window Events");
// Set up window event handlers
mainStage.setOnCloseRequest(this::handleCloseRequest);
mainStage.setOnShowing(this::handleShowing);
mainStage.setOnShown(this::handleShown);
// Display the stage
mainStage.show();
}
private void handleCloseRequest(WindowEvent event) {
System.out.println("Window close requested: " + event.getEventType());
}
private void handleShowing(WindowEvent event) {
System.out.println("Window is showing: " + event.getEventType());
}
private void handleShown(WindowEvent event) {
System.out.println("Window has been shown: " + event.getEventType());
}
}
When you run this application and close the window, the console output will be:
Window is showing: WINDOW_SHOWING
Window has been shown: WINDOW_SHOWN
Window close requested: WINDOW_CLOSE_REQUEST
Keyboard Event Handling
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class KeyboardEventsDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("JavaFX Keyboard Events");
// Create UI element to display keyboard input
Text inputDisplay = new Text("Press any key...");
inputDisplay.setX(50);
inputDisplay.setY(50);
// Set up keyboard event handler
inputDisplay.setOnKeyPressed(this::handleKeyPress);
// Create scene and set it to stage
Pane root = new Pane(inputDisplay);
Scene scene = new Scene(root, 400, 300);
stage.setScene(scene);
// Show the stage
stage.show();
}
private void handleKeyPress(KeyEvent event) {
KeyCode keyCode = event.getCode();
System.out.println("Key pressed: " + keyCode);
// Handle specific keys
if (keyCode == KeyCode.ESCAPE) {
System.out.println("Escape key was pressed");
}
}
}