我想其中一个可能的解决方案是添加以下方法
TestController.java
public void handleWindowShownEvent()
{
usernameInput.requestFocus();
}
</code>
然后改变
start
方法
TestLauncher
以下内容:
@Override
public void start(Stage stage) throws Exception
{
FXMLLoader loader = new FXMLLoader();
Parent root = (Parent)loader.load(TestController.class.getResourceAsStream(“TestView.fxml”));
final TestController controller = (TestController)loader.getController();
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler()
{
@Override
public void handle(WindowEvent window)
{
controller.handleWindowShownEvent();
}
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
</code>
我真的很欢迎其他解决方案,因为这个看起来太笨重……