public class MainApp extends Application { public Stage primaryStage = new Stage(); ImageView iv1; public void start( Stage primaryStage) { AnchorPane root= new AnchorPane(); Scene scene1 = new Scene(root,600,400); primaryStage.setTitle("SocOpros"); Image img = new Image("bgg.png"); iv1 = new ImageView(); iv1.setImage(img); iv1.setVisible(true); Button btn = new Button("TEST");//созданиС ΠΊΠ½ΠΎΠΏΠΊΠΈ btn.setVisible(true); btn.setLayoutX(20); btn.setLayoutY(40); root.getChildren().addAll(iv1,btn); btn.setOnAction(new RotateImg());//установка дСйствия Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ primaryStage.setScene(scene1); primaryStage.show(); } public static void main(String[] args) { launch(args); } public class RotateImg implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent arg0) { Stage stage = new Stage(); AnchorPane root2= new AnchorPane(); Scene scene2 = new Scene(root2,600,400); stage.setScene(scene2); stage.show(); } } } 

When you click on the button, a new window appears as needed. But it is also necessary to close the previous window, but I do not know how to make the primary stage a global object, so that it can work with it in the RotateImg method.

    1 answer 1

    Option 1: Change the listener so that when opening the window it closes the previous one:

     btn.setOnAction(lsn -> { new RotateImg(); primaryStage.close(); }); 

    Option 2: You write "so that you can work with it in the RotateImg method," but you do not have a method there but an internal class, if you replace it with a method, then you can pass the Stage to it and close it, or do anything with it this method:

     public class JavaFXApplication23 extends Application { public Stage primaryStage = new Stage(); ImageView iv1; public void start( Stage primaryStage) { AnchorPane root= new AnchorPane(); Scene scene1 = new Scene(root,600,400); primaryStage.setTitle("SocOpros"); Image img = new Image("bgg.png"); iv1 = new ImageView(); iv1.setImage(img); iv1.setVisible(true); Button btn = new Button("TEST");//созданиС ΠΊΠ½ΠΎΠΏΠΊΠΈ btn.setVisible(true); btn.setLayoutX(20); btn.setLayoutY(40); root.getChildren().addAll(iv1,btn); btn.setOnAction(rotat ->{//ΠΊΠ½ΠΎΠΏΠΊΠ° Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ исполняСт ΠΌΠ΅Ρ‚ΠΎΠ΄ ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ rotateImg(primaryStage);//ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ пСрСдаСтся сцСна Π² качСствС Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° }); primaryStage.setScene(scene1); primaryStage.show(); } public static void main(String[] args) { launch(args); } public void rotateImg(Stage stg){ stg.close();//Π·Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΡƒΡŽ сцСну. Stage stage = new Stage(); AnchorPane root2= new AnchorPane(); Scene scene2 = new Scene(root2,600,400); stage.setScene(scene2); stage.show(); } } 
    • Thank you very much, but in the first method, the window closes first and the second does not open. The second method is fully working. - Dmitriy Mironov