web123456

A preliminary study on JavaFx fxml

1. Controller is connected to fxml

<GridPane fx:controller=""
          xmlns:fx="/fxml" alignment="center" hgap="10" vgap="10">
        <Label ="0" ="0"></Label>
        <TextField fx:id="text" ="0" ="1"></TextField>
        <Button fx:id="button" ="1" prefWidth="50" onAction="#fun">Click me</Button>
</GridPane>

In the controller:

public class Controller {
    @FXML private Button button;
   // @FXML private Text text;
    @FXML public void fun(){
        System.out.println("hello");
    }
}

Get the value entered in TextField:

public class Controller {
    @FXML private Button button;
    @FXML private TextField text;
    @FXML public void fun(){
        System.out.println(text.getText());
    }
}


If TextField is accidentally written as Text, the following will be reportedabnormal

Caused by: javafx.: 
/Users/vyner/Documents/javarepo/beaver/GameFirst/out/production/GameFirst/view/sample.fxml:11


at (:2601)
at (:2579)
at (:2441)
at (:3214)
at (:3175)
at (:3148)
at (:3124)
at (:3104)
at (:3097)
at (:53)
at .lambda$launchApplication1$162(:863)
at $runAndWait$175(:326)
at $null$173(:295)
at (Native Method)
at $runLater$174(:294)
at $(:95)
Caused by: : Can not set field to
at (:167)
at (:171)
at (:81)
at (:764)
at (:1163)
at $1600(:103)
at $(:857)
at $(:751)
at (:2707)
at (:2527)
... 14 more
Exception running application