how to achieve one more widows by javafx
To achieve multiple windows in JavaFX, you can follow these steps:
-
Create a new class for the additional window you want to open. Let's call it "NewWindow".
-
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);
}
}
- 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);
}
}
- Run your main application, and when you click the "Open New Window" button, a new window will open with the "Close Window" button
原文地址: https://www.cveoy.top/t/topic/h8tQ 著作权归作者所有。请勿转载和采集!