我正在学习JavaFX。在主方法中,我只能看到一个launch(args)方法。当我调试进入launch方法时,我看不到任何调用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);
}
}
launch
方法会将事件放到事件队列中,以便调用start
方法,并确保start
方法在其GUI线程的上下文中被调用,无论JavaFX如何称呼它。 - MadProgrammerLauncherImpl.launchApplication()
函数中的 try/catch 块? - awksp