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

  1. Create a new JavaFX project in your preferred IDE.

  2. In the main class, extend the javafx.application.Application class and override the start() method.

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

public class DoubleWindows extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Create the first window
        StackPane root1 = new StackPane();
        Button button1 = new Button("Open Second Window");
        root1.getChildren().add(button1);

        Scene scene1 = new Scene(root1, 300, 200);
        primaryStage.setTitle("First Window");
        primaryStage.setScene(scene1);
        primaryStage.show();

        // Create the second window
        Stage secondaryStage = new Stage();
        StackPane root2 = new StackPane();
        Button button2 = new Button("Close Second Window");
        root2.getChildren().add(button2);

        Scene scene2 = new Scene(root2, 300, 200);
        secondaryStage.setTitle("Second Window");
        secondaryStage.setScene(scene2);
        secondaryStage.show();

        // Handle button actions
        button1.setOnAction(e -> secondaryStage.show());
        button2.setOnAction(e -> secondaryStage.close());
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  1. In the start() method, create two instances of the Stage class. These will represent your windows.

  2. Create the necessary UI components for each window and add them to their respective root layout (e.g., StackPane).

  3. Create Scene objects for both windows, specifying the root layout and the desired width and height.

  4. Set the title of each window using stage.setTitle("Window Title").

  5. Set the scene of each window using stage.setScene(scene).

  6. Show both windows using stage.show().

  7. Handle button actions to control the visibility of the second window. In the example above, clicking the "Open Second Window" button on the first window will show the second window, and clicking the "Close Second Window" button on the second window will close it.

  8. Run the application and you should see two windows displayed

how to achieve double widows by javafx

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

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