[英]Can't seem to get my JavaFX to run successfully. I'm using JRE 10.0.2 as required for my class
enter image description hereNo other indications tell me the code is wrong except for when I try to run my code I get these errors.在此处输入图像描述没有其他迹象告诉我代码错误,除了当我尝试运行我的代码时出现这些错误。
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(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: StackPane@4df22f20is already inside a scene-graph and cannot be set as root
at javafx.graphics/javafx.scene.Scene$8.invalidated(Unknown Source)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
at javafx.graphics/javafx.scene.Scene.setRoot(Unknown Source)
at javafx.graphics/javafx.scene.Scene.<init>(Unknown Source)
at javafx.graphics/javafx.scene.Scene.<init>(Unknown Source)
at application.Shapes.start(Shapes.java:64)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Exception running application application.Shapes
package application;
import javafx.application.Application;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.geometry.Insets;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
public class Shapes extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
//create rectangle and set properties
Rectangle rectangle = new Rectangle();
rectangle.setX(50);
rectangle.setY(50);
rectangle.setWidth(100);
rectangle.setHeight(100);
rectangle.setStroke(Color.BLUE);
rectangle.setStrokeWidth(3);
rectangle.setFill(Color.BLUE);
//create circle and set properties
Circle circle = new Circle(50);
circle.setStroke(Color.RED);
circle.setStrokeWidth(3);
circle.setFill(Color.RED);
//create cross and set properties
Line vertical = new Line(-50 / 1.5, -50 / 2.5, 50 / 1.5, -50 / 2.5);
Line horizontal = new Line(0, -50, 0, 50);
horizontal.setStroke(Color.GREEN);
horizontal.setStrokeWidth(6);
vertical.setStroke(Color.GREEN);
vertical.setStrokeWidth(6);
//create pane to hold cross
StackPane cross = new StackPane(vertical, horizontal);
//create grid and set its properties
GridPane grid = new GridPane();
grid.setPadding(new Insets(5, 5, 5, 5));
//add shapes to grid
grid.add(cross, 2, 2);
grid.add(rectangle, 0, 0);
grid.add(circle, 1, 1);
grid.add(new Group(vertical, horizontal), 2, 2);
//create scene and add pane
Scene newScene = new Scene(grid);
//set name
primaryStage.setTitle("Shapes");
//add the scene to the stage
primaryStage.setScene(newScene);
//display stage
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I got it.我知道了。 I removed //BorderPane root = new BorderPane();
我删除了 //BorderPane root = new BorderPane(); Scene scene = new Scene(root,400,400);
场景场景 = 新场景(root,400,400); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene);
primaryStage.setScene(场景); primaryStage.show();//
主舞台.show();//
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.