在将按钮添加到布局时获取java.lang.reflect.InvocationTargetException [英] getting java.lang.reflect.InvocationTargetException while adding a button to layout
问题描述
我在javafx中是全新的!! 我尝试了一个非常非常简单的代码,但被卡住了. 当我尝试将按钮添加到布局时,它不起作用. 我知道这个问题可能太简单了,但我真的不知道如何解决. 如果您能帮助我,我将不胜感激. 这是我的代码:
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();
}
}
我得到了错误:
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
推荐答案
从错误中我假设您正在使用JavaFX 11(或至少是JavaFX 9+).请注意,最终问题不是IllegalAccessError
:
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
这是告诉您未命名模块"中的代码正在尝试访问javafx.graphics
模块的内部.由于不允许这样做,因此会引发错误.但是这里真正的问题是ControlHelper
在未命名的模块中,但是应该在javafx.controls
模块中,前提是要使用软件包名称.此问题是由于在 modulepath 上具有javafx.graphics
而在 classpath 上具有javafx.controls
引起的.
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.
确保在--module-path
上同时具有两个模块(和javafx.base
).由于您的代码不是模块化的(没有module-info
文件),因此您还必须告诉模块系统使用以下方法来解决该问题:
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
您不必在--add-modules
命令中包含javafx.graphics
,因为javafx.controls
需要javafx.graphics
(而javafx.graphics
需要javafx.base
).由于所有模块都位于模块路径上,因此它们将得到解决.
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.
如何设置这些命令取决于启动应用程序的方式(例如,命令行,IDE,Maven,Gradle等).
How you set these commands is dependent on how you launch your application (e.g. command-line, IDE, Maven, Gradle, etc...).
如果您曾经使代码模块化,则不需要使用--add-modules
命令,只需将相应的requires
指令放入您的module-info
文件中.例如:
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屋!