在向布局添加按钮时获取 java.lang.reflect.InvocationTargetException [英] getting java.lang.reflect.InvocationTargetException while adding a button to layout
问题描述
我是 javafx 的新手!!我尝试了一个非常非常简单的代码,但我被卡住了.当我尝试向布局添加按钮时它不起作用.我知道这个问题可能太简单了,但我真的不知道如何解决.如果你能帮助我,我将不胜感激.这是我的代码:
import javafx.application.Application;导入 javafx.scene.Scene;导入 javafx.scene.control.Button;导入 javafx.scene.layout.StackPane;导入 javafx.stage.Stage;公共类测试扩展应用程序{按钮按钮;公共静态无效主(字符串 [] args){发射(参数);}@覆盖公共无效开始(阶段阶段)抛出异常{stage.setTitle("标题");StackPane 布局 = new StackPane();按钮 = 新按钮();layout.getChildren().add(button);场景场景 = 新场景(布局);stage.setScene(场景);舞台表演();}}
我得到了错误:
应用程序启动方法中的异常java.lang.reflect.InvocationTargetException在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.base/java.lang.reflect.Method.invoke(Method.java:566)在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.base/java.lang.reflect.Method.invoke(Method.java:566)在 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)引起:java.lang.RuntimeException:应用程序启动方法中的异常在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)在 java.base/java.lang.Thread.run(Thread.java:834)引起:java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x46b3f4cf) cannot access class com.sun.javafx.scene.layout.RegionHelper (in modulejavafx.graphics) 因为模块 javafx.graphics 不会将 com.sun.javafx.scene.layout 导出到未命名的模块 @0x46b3f4cf在 java.base/java.lang.ClassLoader.defineClass1(Native Method)在 java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)在 java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)在 java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)在 java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)在 javafx.scene.control.Control.<clinit>(Control.java:86)在 Test.start(Test.java:21)在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)在 java.base/java.security.AccessController.doPrivileged(Native Method)在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)在 javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法)在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)... 1个异常运行应用程序测试
我假设您使用的是 JavaFX 11(或至少 JavaFX 9+)的错误.请注意,最终的问题是不是InvocationTargetException
,而是IllegalAccessError
:
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x46b3f4cf) cannot access class com.sun.javafx.scene.layout.RegionHelper(在模块 javafx.graphics 中)因为模块 javafx.graphics 不会将 com.sun.javafx.scene.layout 导出到未命名的模块 @0x46b3f4cf
这告诉您未命名模块"中的代码正在尝试访问 javafx.graphics
模块的内部结构.由于这是不允许的,因此会引发错误.但这里真正的问题是 ControlHelper
在未命名的模块中,但 应该 在 javafx.controls
模块中,如果包名有什么好去的.此问题是由 javafx.graphics
在 modulepath 上而 javafx.controls
在 classpath 上引起的.>
确保 --module-path
上有两个模块(和 javafx.base
).由于您的代码不是模块化的(没有 module-info
文件),您还必须告诉模块系统通过使用:
--add-modules javafx.controls
您不必在 --add-modules
命令中包含 javafx.graphics
因为 javafx.controls
需要 javafx.graphics
(和 javafx.graphics
需要 javafx.base
).由于所有模块都在模块路径上,它们将被解析.
您如何设置这些命令取决于您启动应用程序的方式(例如命令行、IDE、Maven、Gradle 等...).
如果您使代码模块化,则不需要使用 --add-modules
命令,只需在 中放入适当的
文件.例如:requires
指令模块信息
模块应用{需要 javafx.controls;}
I am totally new in javafx!! I tried a very very simple code and I got stuck. when I tried to add a button to the layout It does not work. I know this question may be too simple but I really do not know how to fix it. I would appreciated if you could help me. This is my code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application{
Button button;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Title");
StackPane layout = new StackPane();
button = new Button();
layout.getChildren().add(button);
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
}
and I got the error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x46b3f4cf) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0x46b3f4cf
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javafx.scene.control.Control.<clinit>(Control.java:86)
at Test.start(Test.java:21)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application Test
From the error I'm assuming you're using JavaFX 11 (or at least JavaFX 9+). Note that the ultimate problem is not the InvocationTargetException
but the IllegalAccessError
:
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x46b3f4cf) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0x46b3f4cf
This is telling you code in the "unnamed module" is trying to access internals of the javafx.graphics
module. As this is not allowed the error is thrown. But the real problem here is that ControlHelper
is in the unnamed module but it's supposed to be in the javafx.controls
module, if the package name is anything to go by. This problem is caused by having javafx.graphics
on the modulepath but javafx.controls
on the classpath.
Make sure you have both modules (and javafx.base
) on the --module-path
. As your code is not modular (no module-info
file) you will also have to tell the module system to resolve it by using:
--add-modules javafx.controls
You don't have to include javafx.graphics
in the --add-modules
command because javafx.controls
requires javafx.graphics
(and javafx.graphics
requires javafx.base
). As all modules are on the modulepath they will be resolved.
How you set these commands is dependent on how you launch your application (e.g. command-line, IDE, Maven, Gradle, etc...).
If you ever make your code modular you won't need to use the --add-modules
command, just put the appropriate requires
directives in your module-info
file. For example:
module app {
requires javafx.controls;
}
这篇关于在向布局添加按钮时获取 java.lang.reflect.InvocationTargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!