简体   繁体   中英

Can't run simple JavaFX programs

I decided to play around with JavaFX since all I've ever known graphics-wise is Swing, since that's what I learned in my early college days, and that's what I've been using for developing games. However, I've had some trouble getting started, and was hoping someone has encountered these issues before so I can correct them.

When I first started, I opened Eclipse (Mars) expecting to be able to just jump right into it, since it's a core library, but when I saw syntax error highlighting on the imports for the library, it seems that Eclipse (or Java) doesn't allow you to access the jfxrt.jar library. After some Googling, I was able to correct this by downloading the e(fx)clipse plugin for Eclipse. This allowed me to create a very simple JavaFX application that just shows a blank window.

However, when trying to render a simple graphics primitive (a rectangle), I got an exception that Google seems to indicate is a problem within the API itself. Has anyone encountered this exception before, and is there something I can do to get JavaFX working correctly so I can actually start playing around with it?

Exception in Application start method java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Children: cycle detected: parent = Group@1c73f0a[styleClass=root], node = Group@1c73f0a[styleClass=root]
    at javafx.scene.Parent$2.onProposedChange(Parent.java:445)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at FXMain.start(FXMain.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application FXMain

Here is my program...

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FXMain extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Java FX Example");

        Group root = new Group();
        Scene scene = new Scene(root);
        stage.setScene(scene);

        Canvas canvas = new Canvas(640, 480);
        root.getChildren().add(root);

        GraphicsContext gc = canvas.getGraphicsContext2D();

        gc.setFill(Color.RED);
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(2);
        gc.fillRect(10, 10, 50, 50);
        gc.strokeRect(10, 10, 50, 50);

        stage.show();
    }
}

What's this do?

root.getChildren().add(root);

Check the line of the error where it's your program, not java classes. (most ide's you can just click on it)

This line. at FXMain.start(FXMain.java:23)

I didn't count but I'm assuming just a mis-type. You probably want to add the canvas. Not sure you need the canvas anyway, you draw with nodes mainly in JavaFX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM