简体   繁体   English

如何将 Swing JPanel 嵌入到 JavaFX 框架中?

[英]How do you embed a Swing JPanel into a JavaFX frame?

From Integrating JavaFX 2.0 with Swing and SWT there is a code that allows you to embed a JFXPanel inside a Swing JFrame. I was wondering is there a reverse of that where a JPanel is embedded inside an Application or Stage which is the equivalent of JFrame as noted in Converting from Swing to JavaFX?Integrating JavaFX 2.0 with Swing and SWT有一段代码允许您将 JFXPanel 嵌入 Swing JFrame 中。我想知道 JPanel 嵌入ApplicationStage中的情况是否与此相反,相当于JFrame从 Swing 转换为 JavaFX 中注意到了吗?

/**
 * Simple class demonstrating interoperability between Swing and JavaFX. This
 * class is adapted from the example provided in the Javadoc documentation for
 * {@code javafx.embed.swing.JFXPanel}.
 */
public class SwingJavaFxInteroperabilityDemo
{
   private static void initAndShowGUI()
   {
      // This method is invoked on Swing thread
      final JFrame frame = new JFrame("JavaFX / Swing Integrated");
      final JFXPanel fxPanel = new JFXPanel();
      frame.add(fxPanel);
      frame.setVisible(true);

      Platform.runLater(new Runnable()
      {
         @Override
         public void run()
         {
            initFX(fxPanel);
         }
      });
   }

   private static void initFX(JFXPanel fxPanel)
   {
      // This method is invoked on JavaFX thread
      final Scene scene = TextIntegrationSceneCreator.createTextScene();
      fxPanel.setScene(scene);
   }

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

You can use a SwingNode to place swing content into a JavaFX node.您可以使用SwingNode将 swing 内容放入 JavaFX 节点。

Sample Application示例应用程序

There is an example application in the SwingNode documentation previously linked:之前链接的 SwingNode 文档中有一个示例应用程序:

图片

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing.*;

public class SwingFx extends Application {

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

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

        stage.setScene(new Scene(pane, 100, 50));
        stage.show();
    }

    private void createAndSetSwingContent(
        final SwingNode swingNode
    ) {
        SwingUtilities.invokeLater(() -> 
                swingNode.setContent(
                        new JButton("Click me!")
                )
        );
    }

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

Dependency and Module Management依赖和模块管理

If you are using Maven or Gradle for your JavaFX dependencies, you can import the library for JavaFX/Swing integration using:如果您将 Maven 或 Gradle 用于 JavaFX 依赖项,则可以使用以下方法导入 JavaFX/Swing 集成库:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-swing</artifactId>
    <version>16</version>
</dependency>

If you are writing a modular application using Jigsaw capabilities, placing the following in your module-info.java file will make the JavaFX swing integration module visible to your application:如果您使用 Jigsaw 功能编写模块化应用程序,将以下内容放入您的module-info.java文件将使 JavaFX swing 集成模块对您的应用程序可见:

requires javafx.swing;

Embedding Swing in JavaFX Tutorial在JavaFX教程中嵌入Swing

Oracle provides a good tutorial for embedding Swing content in JavaFX: Oracle 提供了一个很好的在JavaFX中嵌入Swing内容的教程:

The tutorial is written for Java 8 but is still relevant.本教程是为 Java 8 编写的,但仍然适用。 The tutorial covers topics such as thread management and event handling, and I advise anybody attempting the integration of JavaFX and Swing to understand those topics very well before attempting integration, otherwise bad and unpredictable things may occur.该教程涵盖了线程管理和事件处理等主题,我建议任何尝试集成 JavaFX 和 Swing 的人在尝试集成之前非常了解这些主题,否则可能会发生糟糕和不可预测的事情。

In general, I advise not mixing JavaFX and Swing code unless that is absolutely necessary for your application.一般来说,我建议不要混合使用 JavaFX 和 Swing 代码,除非这对您的应用程序来说是绝对必要的。

Canvas Integration and Heavyweight Swing Components Canvas 集成和重量级 Swing 组件

Note, from the SwingNode documentation:请注意,来自 SwingNode 文档:

The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise, SwingNode may fail to paint it. JComponent实例包含的组件层级不应包含任何重量级组件,否则SwingNode可能无法绘制它。

An example of a heavyweight component is a Canvas .重量级组件的一个示例是Canvas If you wish to use an awt canvas in JavaFX, see:如果您想在 JavaFX 中使用 awt canvas,请参阅:

Specifically, consider using the FXGraphics2D library:具体来说,考虑使用FXGraphics2D库:

FXGraphics2D is an implementation of Java2D's Graphics2D API that targets a JavaFX canvas. FXGraphics2D 是 Java2D 的 Graphics2D API 的实现,目标是 JavaFX canvas。


Maven doesn't know a javafx.embed.swing. Maven 不知道 javafx.embed.swing。 Any idea where to find it?知道在哪里可以找到它吗?

This should not be the case for a properly configured Maven setup.对于正确配置的 Maven 设置,情况不应如此。

All of the JavaFX framework modules are in the central maven repository .所有 JavaFX 框架模块都在中央 maven 存储库中。

The central maven repository is enabled by default in a standard Maven installation.中央 maven 存储库在标准 Maven 安装中默认启用。

The maven coordinates for javafx-swing are already in this answer (under the section titled "Dependency and Module Management"). javafx-swing 的 maven 坐标已经在这个答案中(在标题为“依赖和模块管理”的部分下)。

Update the dependency version to match the JavaFX version you are using.更新依赖版本以匹配您正在使用的 JavaFX 版本。

The recent stable version of the dependency is 18.0.1 today, so to use that, ensure that all javafx components that you rely on use that version for their JavaFX dependencies.依赖项的最新稳定版本今天是18.0.1 ,因此要使用它,请确保您依赖的所有javafx 组件都使用该版本作为其 JavaFX 依赖项。

Alternately, you can或者,您可以

However, I recommend using dependencies on your Maven or Gradle build tools rather than relying on manually downloaded modules or the JavaFX SDK.但是,我建议使用 Maven 或 Gradle 构建工具的依赖项,而不是依赖手动下载的模块或 JavaFX SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM