1、Controler 与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">点我</Button> </GridPane>
controler中:
public class Controller { @FXML private Button button; // @FXML private Text text; @FXML public void fun(){ System.out.println("hello"); } }
取得TextField中输入的值:
public class Controller { @FXML private Button button; @FXML private TextField text; @FXML public void fun(){ System.out.println(text.getText()); } }
若是TextField不小心写成Text会报以下异常:
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