无法从Eclipse运行JavaFx代码 [英] Can't run JavaFx code from Eclipse

查看:306
本文介绍了无法从Eclipse运行JavaFx代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上发现了这个代码oracle但是我不能用Eclipse启动它说我无法启动。我已经安装了JDK 8但是它不起作用...

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...

有人有解决方案吗? :p

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!"));
        }
    });
}
}

链接到我找到代码的网站:< a href =https://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm =nofollow> https://docs.oracle.com/javafx/8/embed_swing/jfxpub- embed_swing.htm

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

推荐答案

在Java 8中,您可以直接启动 javafx.application .Application 子类,即使它没有 main(String [] args)方法。问题是当前版本的Eclipse没有检查这一点,并且(至少在上下文菜单中)仅检查是否存在 main(...)方法。因此,要允许它从Eclipse运行,您可以自己添加 main(...)方法:

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);
    }
}

或者,你可以直接指示Eclipse从中运行它运行配置向导。选择 SwingFX 类后,从菜单中选择运行,然后选择运行配置。在Main选项卡中,确保显示正确的类名(即SwingFX),然后按运行按钮。

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 (i.e. "SwingFX") and then press the "Run" button.

一旦设置完毕,工具栏上的绿色运行按钮(在Java透视图中)将再次运行应用程序,直到您运行其他内容。

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运行JavaFx代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆