To achieve multiple windows in JavaFX, you can follow these steps:

  1. Create a new class for the additional window you want to open. Let's call it "NewWindow".

  2. In the NewWindow class, extend the JavaFX Stage class and implement the necessary UI components and logic for the new window.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewWindow extends Stage {
    public NewWindow() {
        Button button = new Button("Close Window");
        button.setOnAction(event -> close());

        StackPane root = new StackPane(button);
        Scene scene = new Scene(root, 300, 200);
        setScene(scene);
    }
}
  1. In your main JavaFX application, create a button or any UI element that will trigger the opening of the new window.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainApp extends Application {
    public void start(Stage primaryStage) {
        Button openButton = new Button("Open New Window");
        openButton.setOnAction(event -> {
            NewWindow newWindow = new NewWindow();
            newWindow.show();
        });

        StackPane root = new StackPane(openButton);
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  1. Run your main application, and when you click the "Open New Window" button, a new window will open with the "Close Window" button
how to achieve one more widows by javafx

原文地址: https://www.cveoy.top/t/topic/h8tQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录