0
我是最近剛接觸javafx的人,我總是在控制器類的構造函數中加載fxml,然後直接使用組件。JavaFX FXML構造函數和初始化方法
我剛纔瞭解到組件只能在調用initialize之前訪問。
但是,即使我在構造函數中使用組件,也不會出現任何錯誤。 我的代碼看起來像這樣。
@Override
public void start(Stage primaryStage){
new MainController(primaryStage,this);
}
public class MainController{
@FXML
private ListView<HistoryPlay> historyLV;
public MainController(Stage primaryStage, Main main) {
initFxml();
initView();
}
private void initFxml() {
FXMLLoader loader=new FXMLLoader();
loader.setController(this);
try {
loader.setLocation(new File("fxml\\Main.fxml").toURL());
loader.load();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void initView() {
historyLV.setCellFactory(param -> new HistoryListCell(MainController.this,main));
historyLV.setItems(main.getHistoryManager().getHistoryList());
}
}
沒有零點例外occur.Why?