具有透明内容的 SwingNode

SwingNode with transparent content

我正在尝试将 Swing 内容嵌入到更大的 JavaFX 应用程序中,但是我无法让嵌入的 JPanel 具有透明背景。 JPanel 仅绘制其部分区域。我想要绿色为 "show through",即移除下图中的浅灰色背景。

public class TransparentSwingNode extends Application {

    @Override
    public void start(Stage stage) {
        BorderPane pane = new BorderPane();
        pane.setStyle("-fx-background-color: green");
        final SwingNode swingNode = new SwingNode();
        BorderPane.setMargin(swingNode, new Insets(20));
        createAndSetSwingContent(swingNode);
        pane.setCenter(swingNode);
        // swingNode.setStyle("-fx-background-color: transparent"); // doesn't work

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

    private void createAndSetSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JPanel panel = new DummyPanel();
                panel.setOpaque(false);
                // panel.setBackground(null); // doesn't work
                // panel.setBackground(new Color(0, 0, 0, 0)); // Doesn't work
                swingNode.setContent(panel);
            }
        });
    }

    private class DummyPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(getHeight() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
        }
    }

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

我试过了

更新

我的同事终于找到了一些东西。系统属性swing.jlf.contentPaneTransparent,即-Dswing.jlf.contentPaneTransparent=true

结果

除了

,我找不到任何官方文档

Also, we have an experimental undocumented option ("swing.jlf.contentPaneTransparent") to switch on SwingNode's transparency. That's to say that we probably shouldn't refuse transparency support, unless this requires serious efforts.