AWT 面板未在 JFX 中呈现

AWT Panel not getting rendered in JFX

我想向我的 JavaFX8 应用程序添加一个 java.awt.Panel。不幸的是,当附加到 SwingNode.

时,面板似乎没有被渲染

我有一个简单的测试应用程序:

import java.awt.Dimension;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AWTInJFX extends Application {

    @Override
    public void start(Stage stage) {

        final AwtInitializerTask awtInitializerTask = new AwtInitializerTask(() -> {
                AWTPanel panel = new AWTPanel();
                return panel;
        });

        SwingNode swingNode = new SwingNode();

        SwingUtilities.invokeLater(awtInitializerTask);
        try {
            swingNode.setContent(awtInitializerTask.get());
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(AWTInJFX.class.getName()).log(Level.SEVERE, null, ex);
        }

        stage.setScene(new Scene(new Group(swingNode), 600, 600));
        stage.setResizable(false);
        stage.show();       

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setSize(new Dimension(600, 400));
            frame.add(new AWTPanel());
            frame.setVisible(true);
        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private class AwtInitializerTask extends FutureTask<JPanel> {
        public AwtInitializerTask(Callable<JPanel> callable) {
            super(callable);
        }
    }

}

我的 JPanel 包含 java.awt.Panel

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import javax.swing.JPanel;

public class AWTPanel extends JPanel{
    public AWTPanel()
    {
        Dimension d = new Dimension(600, 400);
        setPreferredSize(d);
        Panel p = new Panel();
        p.setSize(d);
        p.setPreferredSize(d);
        p.setBackground(Color.red);
        this.add(p);
        this.setBackground(Color.green);
    }
}

当我将 AWTPanel 添加到 SwingNode 时,其他 AWT 组件也没有显示。

有人可以向我解释为什么这不起作用吗?

我需要一个 AWT 面板来获得用于其他 C++ 库的 hWnd。

来自SwingNode Javadocs

The hierarchy of components contained in the JComponent instance should not contain any heavyweight components, otherwise SwingNode may fail to paint it.

据我所知,没有办法在 JavaFX 中嵌入重量级组件,例如 AWT 组件。

根据您的要求,您可以考虑颠倒过来,这样您就有一个 Swing/AWT 框架作为您的主要 window,并将应用程序的 JavaFX 部分嵌入到 JFXPanel.