将JFXPanel添加到JFrame。为什么不起作用? [英] Adding JFXPanel to a JFrame. Why is it not working?

查看:121
本文介绍了将JFXPanel添加到JFrame。为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JFXPanel添加到我的JFrame。我正在使用Netbeans和Netbeans的Swing GUI Builder。当我运行它时,我没有收到任何错误,并且成功创建了JFrame,但是未显示带有文本的JFXPanel。

I am trying to add a JFXPanel to my JFrame. I am using Netbeans and Netbeans' Swing GUI Builder. When I run it I do not get any errors and the JFrame is created successfully but the JFXPanel with the text does not appear.

这是我的代码:

public class TestingFX {

  private static void initJFrame() {

    NewJFrame frame = new NewJFrame();
    final JFXPanel fxPanel = new JFXPanel();
    frame.add(fxPanel);
    frame.setVisible(true);

    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            fxPanel.setScene(createScene());
        }
    });
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            initJFrame();
        }
    });
  }

  public static Scene createScene() {
    Text text = new Text("Hello World");
    text.setFont(new Font(24));
    text.setEffect(new Reflection());

    BorderPane pane = new BorderPane();
    pane.setCenter(text);
    Scene scene = new Scene(pane);

    return scene;
  }
}

为什么不起作用?我在做错什么吗?

Why is it not working? Am I doing something wrong?

推荐答案

也许您应该阅读将JavaFX集成到Swing应用程序中,然后相应地重构代码。

Perhaps you should read Integrating JavaFX into Swing Applications, and then refactor your code accordingly.

尝试

public class Demo extends Application{
    private static JFXPanel javafxPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }

    public static void initAndShowGUI() {
        JFrame frame = new JFrame("Swing application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create JavaFX panel.
        javafxPanel = new JFXPanel();
        frame.getContentPane().add(javafxPanel, BorderLayout.CENTER);

        // Create JavaFX scene.
        Application.launch (Demo.class, null);

        // Show frame.
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void start (Stage mainStage) {
        // Add scene to panel
        javafxPanel.setScene(createScene());
    }

    private static Scene createScene() {
        Text text = new Text("Hello World");
        text.setFont(new Font(24));
        text.setEffect(new Reflection());

        BorderPane pane = new BorderPane();
        pane.setCenter(text);
        Scene scene = new Scene(pane);

        return scene;
    }
}






创建场景时,此示例假定您包含的代码有效。


When creating a scene, this example assumes that the code you've included is valid.

这篇关于将JFXPanel添加到JFrame。为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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