无法成功运行我的JavaFX。我正在使用JRE 10.0.2,这是我课程所要求的版本。

huangapple 未分类评论46阅读模式
英文:

Can't seem to get my JavaFX to run successfully. I'm using JRE 10.0.2 as required for my class

问题

在运行代码时,除了以下错误之外,没有其他迹象表明代码有问题。

```none
在Application启动方法中的异常
java.lang.reflect.InvocationTargetException
	在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	在java.base/java.lang.reflect.Method.invoke(Unknown Source)
	在javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
	在javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
	在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	在java.base/java.lang.reflect.Method.invoke(Unknown Source)
	在java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
由于:java.lang.RuntimeException: 在Application启动方法中的异常
	在javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
	在javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
	在java.base/java.lang.Thread.run(Unknown Source)
由于:java.lang.IllegalArgumentException: StackPane@4df22f20 已经位于场景图中,不能设置为根
	在javafx.graphics/javafx.scene.Scene$8.invalidated(Unknown Source)
	在javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
	在javafx.base/javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
	在javafx.graphics/javafx.scene.Scene.setRoot(Unknown Source)
	在javafx.graphics/javafx.scene.Scene.<init>(Unknown Source)
	在javafx.graphics/javafx.scene.Scene.<init>(Unknown Source)
	在application.Shapes.start(Shapes.java:64)
	在javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
	在javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
	在javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
	在java.base/java.security.AccessController.doPrivileged(Native Method)
	在javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
	在javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
	在javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	在javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
	... 还有 1 行
应用程序启动时出现异常 application.Shapes
    包 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();
		
		//创建矩形并设置属性
		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);
		
		//创建圆并设置属性
		Circle circle = new Circle(50);
		circle.setStroke(Color.RED);
		
		circle.setStrokeWidth(3);
		circle.setFill(Color.RED);
		
		//创建十字架并设置属性
		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);
		
		//创建包含十字架的面板
		StackPane cross = new StackPane(vertical, horizontal);
		
		//创建网格并设置其属性
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(5, 5, 5, 5));
		
		//将形状添加到网格
		grid.add(cross, 2, 2);
		grid.add(rectangle, 0, 0);
		grid.add(circle, 1, 1);
		grid.add(new Group(vertical, horizontal), 2, 2);
		
		//创建场景并添加面板
		Scene newScene = new Scene(grid);
		
		//设置标题
		primaryStage.setTitle("Shapes");
		
		//将场景添加到舞台
		primaryStage.setScene(newScene);
		
		//显示舞台
		primaryStage.show();
		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}

<details>
<summary>英文:</summary>

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.

```none
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.&lt;init&gt;(Unknown Source)
	at javafx.graphics/javafx.scene.Scene.&lt;init&gt;(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(&quot;application.css&quot;).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(&quot;Shapes&quot;);
		
		//add the scene to the stage
		primaryStage.setScene(newScene);
		
		//display stage
		primaryStage.show();
		
		
	}
	
	public static void main(String[] args) {
		launch(args);
	}

}

答案1

得分: 0

我明白了。我去掉了 //BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show(); //

英文:

I got it. I removed //BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();//

huangapple
  • 本文由 发表于 2020年4月8日 03:41:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61088153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定