JavaFX程序何时调用start方法?
|
我正在学习
JavaFX.我只能在main方法中看到启动(args)方法.当我调试进入发布时.我看不到任何语句调用start().那么JavaFX程序何时调用start方法?
这是launch(args)源代码. public static void launch(String... args) {
// Figure out the right class to call
StackTraceElement[] cause = Thread.currentThread().getStackTrace();
boolean foundThisMethod = false;
String callingClassName = null;
for (StackTraceElement se : cause) {
// Skip entries until we get to the entry for this class
String className = se.getClassName();
String methodName = se.getMethodName();
if (foundThisMethod) {
callingClassName = className;
break;
} else if (Application.class.getName().equals(className)
&& "launch".equals(methodName)) {
foundThisMethod = true;
}
}
if (callingClassName == null) {
throw new RuntimeException("Error: unable to determine Application class");
}
try {
Class theClass = Class.forName(callingClassName,true,Thread.currentThread().getContextClassLoader());
if (Application.class.isAssignableFrom(theClass)) {
Class<? extends Application> appClass = theClass;
LauncherImpl.launchApplication(appClass,args);
} else {
throw new RuntimeException("Error: " + theClass
+ " is not a subclass of javafx.application.Application");
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
解决方法
LauncherImpl调用Application #start,但是通过PlatformImpl.runAndWait将actuall调用放到JavaFX事件队列中.这是在Preloader启动后完成的
Application#launch调用LauncherImpl.launchApplication,它创建一个Thread并调用launchApplication1,然后launchApplication通过CountDownLatch等待此Thread终止. 然后,此线程调用LauncherImpl.launchApplication1,如果指定,则启动Preloader,然后根据有关Preloader调用Application #start状态的数字决定,包含在runAndWait调用中,以确保在上下文中调用start. JavaFX的GUI /事件队列线程…… 这基于Java 8 更新… 由LauncherImpl.launcherApplication1调用的theApp.start是Application的实例. 应用程序通过遍历StackTrace来查找您的类名…所有事情…… StackTraceElement[] cause = Thread.currentThread().getStackTrace();
boolean foundThisMethod = false;
String callingClassName = null;
for (StackTraceElement se : cause) {
// Skip entries until we get to the entry for this class
String className = se.getClassName();
String methodName = se.getMethodName();
if (foundThisMethod) {
callingClassName = className;
break;
} else if (Application.class.getName().equals(className)
&& "launch".equals(methodName)) {
foundThisMethod = true;
}
}
这将获取您的类的名称,然后使用Class.forName创建一个Class实例并将其传递给LauncherImpl … launcherApplication1然后构造此类的新实例并将其分配给引用theApp,它是应用程序的一个实例 PlatformImpl.runAndWait(new Runnable() {
@Override public void run() {
try {
Constructor<? extends Application> c = appClass.getConstructor();
app.set(c.newInstance());
// Set startup parameters
ParametersImpl.registerParameters(app.get(),new ParametersImpl(args));
PlatformImpl.setApplicationName(appClass);
} catch (Throwable t) {
System.err.println("Exception in Application constructor");
constructorError = t;
error = true;
}
}
});
}
final Application theApp = app.get();
然后它继续调用app.start,它调用你的应用程序的启动方法….我知道很奇怪,但它是 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
