首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >似乎无法让我的JavaFX成功运行。我正在使用JRE 10.0.2作为我的类所需的

似乎无法让我的JavaFX成功运行。我正在使用JRE 10.0.2作为我的类所需的
EN

Stack Overflow用户
提问于 2020-04-08 03:41:50
回答 1查看 30关注 0票数 1

输入图像描述hereNo其他指示告诉我代码是错误的,除非当我尝试运行代码时,我得到这些错误。

代码语言:javascript
复制
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
代码语言:javascript
复制
    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);
    }

}
EN

回答 1

Stack Overflow用户

发布于 2020-04-08 04:05:56

这样啊,原来是这么回事。我删除了//BorderPane根=新场景();场景场景=新场景(根,400,400);scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());primaryStage.setScene(场景);primaryStage.show();//

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61088153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档