Home  >  Article  >  Java  >  Introduction to the method of switching between multiple scenes in javafx (with code)

Introduction to the method of switching between multiple scenes in javafx (with code)

不言
不言forward
2019-03-14 10:52:585366browse

This article brings you an introduction to the method of switching between multiple scenes in javafx (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I was working on a javafx application some time ago and encountered some pitfalls. Record it in this article. (If you have a better solution, please comment, I am a novice, light spray)

1. Problem

According to the official Chinese document, a single interface form login was successfully run. So I wanted to try multi-interface jump myself and add event responses to buttons. However, no matter how I operate, I get an error, and Baidu has been trying for a long time without a solution. Later, Google found a suitable solution.

2. Code

The following code is the button to create a string in the fxml file of my main program interface

 <Button fx:id="CreateString" defaultButton="true" layoutX="216.0" layoutY="159.0" mnemonicParsing="false" onAction="#CreateStringOperation" prefHeight="58.0" prefWidth="154.0" text="创建字符串">
               <font>
                  <Font size="23.0" />
               </font>
            </Button>

This is what I solved by using the following method to pop up another interface through a button action. Among them, CreateString.fxml is the interface layout of the pop-up window.

 //创建字符串
    @FXML protected void CreateStringOperation(ActionEvent event) throws IOException {
        Parent Operation_Parent = FXMLLoader.load(getClass().getResource("CreateString.fxml"));
        Scene Operation_Creating_Scene = new Scene(Operation_Parent);
        Stage CreateOperation_Stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        CreateOperation_Stage.hide();
        CreateOperation_Stage.setScene(Operation_Creating_Scene);
        CreateOperation_Stage.show();
    }

The above is the detailed content of Introduction to the method of switching between multiple scenes in javafx (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete