Ada*_*dam 5 java swing javafx javafx-8
我正在尝试将 Swing 内容嵌入到更大的 JavaFX 应用程序中,但是我无法让嵌入的 JPanel 具有透明背景。JPanel 仅绘制其部分区域。我希望绿色“显示出来”,即删除下图中的浅灰色背景。

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);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了
更新
我的同事终于找到了一些东西。系统属性swing.jlf.contentPaneTransparent,即-Dswing.jlf.contentPaneTransparent=true

除了这个,我找不到任何官方文件
此外,我们有一个实验性的未记录选项(“swing.jlf.contentPaneTransparent”)来打开 SwingNode 的透明度。也就是说,我们可能不应该拒绝透明度支持,除非这需要认真的努力。