简体   繁体   中英

Can't run JavaFx code from Eclipse

I found this code on the site oracle but I can't launch with Eclipse it's saying me "Unable to launch". I have installed JDK 8 but it doesn't work...

Anybody have a solution please ? :p

public class SwingFX extends Application {

@Override
public void start (Stage stage) {
    final SwingNode swingNode = new SwingNode();

    createSwingContent(swingNode);

    StackPane pane = new StackPane();
    pane.getChildren().add(swingNode);

    stage.setTitle("Swing in JavaFX");
    stage.setScene(new Scene(pane, 250, 150));
    stage.show();
    }

private void createSwingContent(final SwingNode swingNode) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            swingNode.setContent(new JButton("Click me!"));
        }
    });
}
}

link to the website where I found the code : https://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm

In Java 8, you can directly launch a javafx.application.Application subclass even if it doesn't have a main(String[] args) method. The issue is that the current version of Eclipse doesn't check for this, and (at least in the context menus) only checks whether the main(...) method exists. So to allow it to run from Eclipse, you can either add the main(...) method yourself:

public class SwingFX extends Application {

    @Override
    public void start(Stage stage) {
        // ...
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Or, you can directly instruct Eclipse to run it from the "Run Configurations" wizard. With your SwingFX class selected, choose "Run" from the menu, and then "Run Configurations". In the "Main" tab make sure the correct class name appears (ie "SwingFX") and then press the "Run" button.

Once you've set this up, the green "Run" button on the toolbar (in the Java perspective) will run the application again, until you run something else.

Eclipse has a problem detecting that JavaFX applications can be launched without a main method in Java 8 - a workaround is to add this yourself

public static void main(String[] args) {
    launch(args);
}

you have not written the main method. thus JVM does not know the starting point

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