当我添加按钮代码时,出现错误,程序在运行时将崩溃。但是当我删除代码 对于该按钮,该程序将运行,并且将仅显示不带任何内容的窗口。我希望显示没有功能的按钮,但给出一个错误。我想念什么吗?请帮忙。
package guiTutorials;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
public class buckyTutorialOne extends Application{
private Button button;
public static void main(String args[]){
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
//Java fx code written here
//the window is called the stage
//content inside the stage is the scene
//the scene is composed of components like widgets, buttons,
etc.\
//layout- how you want everything arranged on your screen
try {
stage.setTitle("Title of the Window"); //helps set stage
button = new Button(); //component for scene sets stage
button.setText("Click Me");
StackPane layout = new StackPane(); //layout sets stage
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);//setting the scene
stage.setScene(scene);
stage.show();
}
catch(Exception e){
e.printStackTrace();
}
}
}
这是我得到的结果:
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:567)
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:567)
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:835)
Caused by: java.lang.IllegalAccessError: superclass access check
failed: class com.sun.javafx.scene.control.ControlHelper
(in unnamed module @0x5f8dfd9d) 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 @0x5f8dfd9d
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:151)
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 guiTutorials.buckyTutorialOne.start(buckyTutorialOne.java:30)
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
(AccessController.java:389)
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)
Exception running application guiTutorials.buckyTutorialOne
Process finished with exit code 1