在这段代码中:
public class ESM extends Application {
private Stage primaryStage;
@FXML
private ToolBar mainToolBar;
@Override
public void start(final Stage stage) throws Exception {
try{
this.primaryStage = stage;
Parent root = FXMLLoader.load(getClass().getResource("/nz/co/great_ape/esm3/main_window.fxml"));
Scene scene = new Scene(root, 800, 700);
// Setup main stage to be full screen, no min or max buttons.
// TODO: How will this handle multiple screens? Apparently not well :-(
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle("ESM three");
primaryStage.setScene(scene);
primaryStage.show();
System.out.println("This will fail because mainToolBar is null. Why?");
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
} catch (Exception ex) {
Logger.getLogger(ESM.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Use initialize() to setup widgets from scenebuilder files, it is
* called by FXMLLoader.
*/
@FXML
public void initialize(){
System.out.println("initialize() But when here all is good and mainToolBar is a ToolBar.");
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
launch(args);
}
Run Code Online (Sandbox Code Playgroud)
}
我不明白为什么它在initialise()中有一个值,但在开始时它是null.在调试时很明显,FXMLLOader从start()内部调用initiialize()
我打算发布fxml但它似乎不能用作预览中的nothig节目.无论如何,它是一个真正的基本文件,BordePane和ToolBar.
有线索吗?
始终为FXML Controller创建一个新类,不要尝试将Application类重用为Controller类.
Application实例由JavaFX应用程序启动器创建.
Controller实例由JavaFX FXML加载程序创建.
你没有提供你使用的FXML,但我猜它将它的Controller类错误地设置为你的应用程序类.
所以在你的代码中,会发生什么:
总之,要解决这个问题:
如果您的应用程序确实需要引用控制器,那么您可以在FXML加载器上使用getController方法,并在控制器类中提供公共方法来检索所需的元素(如菜单栏).有关此方法的更多示例,请参阅我的传递参数JavaFX FXML的答案.
import javafx.scene.control.ToolBar;
import javafx.fxml.FXML;
public class ESMController {
@FXML
private ToolBar mainToolBar;
public ToolBar getMainToolBar() { return mainToolBar; }
@FXML
public void initialize(){
assert mainToolBar != null : "fx:id=\"mainToolBar\" was null check your FXML ";
}
}
Run Code Online (Sandbox Code Playgroud)