如果在控制器中实现Initializable这个接口,并重iInitializable这个方法

对于一个fxml文件来说它首先执行控制器的构造函数,这个时候它是无法对@FXML修饰的方法进行访问的,然后执行@FXML修饰的方法,最后执行initializable方法,我们可以在initializable方法中

对fxml文件的控件进行初始化

 //package application;
package application; import java.io.File;
import java.net.URL;
import java.util.ResourceBundle; import com.sun.prism.paint.Color; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.scene.text.Text;
import javafx.fxml.Initializable;
public class MyController implements Initializable {
@FXML
private Label textOne;
public MyController() {
System.out.print("one method");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
textOne.setText("qwe");
}
}

fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<AnchorPane fx:controller="application.MyController"
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="717.0" prefWidth="824.0"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml">
<children>
<Label alignment="TOP_CENTER" layoutX="60.0" layoutY="139.0"
text="电影排行" />
</children>
</AnchorPane>
05-11 16:28