将 JFXPanel 添加到 JFrame。为什么它不起作用?

发布于 2024-11-24 08:12:25 字数 1024 浏览 4 评论 0原文

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

这是我的代码:

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

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

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.

Here is my code:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡少年 2024-12-01 08:12:25

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


尝试

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

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

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


Try

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.

﹂绝世的画 2024-12-01 08:12:25

我试图将 JFXPanel 添加到 JPanel,这解决了我的问题,也可能对您有帮助

jpanel1.setLayout(new FlowLayout());

I was trying to add JFXPanel to JPanel and this solved the problem for me, might help you as well

jpanel1.setLayout(new FlowLayout());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文