Eclipse未运行JavaFX应用程序-运行'java'应用程序

Eclipse未运行JavaFX应用程序-运行'java'应用程序

问题描述:

每当我在Eclipse中创建一个项目并包含javafx时,单击运行"按钮都不会加载该应用程序.

Whenever I create a project in Eclipse and include javafx, the application does not load when I click the run button.

例如

package test;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

这应该运行一个简单的hello world应用程序,该应用程序取自oracle文档.但是,当我运行"此代码时,没有打开任何窗口.而是打开一个名为"java"的应用程序.看起来"java"只是位于"jdk1.8.0_25.jdk/contents/home/bin"中的"unix可执行文件".应用程序'java'绝对不显示任何内容,并且在不强制退出的情况下无法关闭.

This should run a simple hello world application, taken from the oracle documentation. However when I 'run' this code, no windows open. Instead an application called 'java' opens. It appears 'java' is simply a 'unix executable file' located in 'jdk1.8.0_25.jdk/contents/home/bin'. The application 'java' displays absolutely nothing, and cannot be shut down without force quitting.

我正在Macbook上运行Eclipse.我可能遗漏了一些重要的细节...

I'm running eclipse on a Macbook. I've probably left out some important details...

有人知道为什么我的应用程序无法正常运行吗?原谅我的天真,我是java和eclipse的新手.

Does anyone know why my application is not running as it should? Forgive my naivety, I'm new to java and eclipse.

非常感谢

import javafx.application.Application;
import javafx.stage.Stage;


public class JavaFX extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        stage.show();

    }

}

这个简单的程序也给出相同的错误.

This simple program also gives the same error.

所以我知道这是一个老问题,但是最近我遇到了相信这个问题,并希望分享找到的解决方案(尽管我对为什么起作用没有任何见识).

So I know this is an old question, but I ran into what I believe to be the same issue recently and wanted to share the solution I found (although I have no insight as to why it works).

如图

转到主类的运行配置",然后在参数"选项卡上,取消选中显示使用SWT启动时使用-XstartOnFirstThread参数"的框.

Go to Run Configurations for the main class, and on the "Arguments" tab, uncheck the box where it says "Use the -XstartOnFirstThread argument when launching with SWT".

添加一个直观的问题示例,以便比我可能更了解的人解释为什么会发生这种情况/为什么此解决方案有效:

To add a visual example of the problem so that someone more knowledgable than I can possibly explain why this happens/why this solution works:

是您尝试运行程序时得到的.名为"java"的应用程序似乎正在运行,但未显示任何内容.

This is what you get when you try to run the program. An application simply called "java" appears to be running, but nothing is showing.

我希望这些信息能够对某人有所帮助.

I hope this information is able to help someone.