我正在尝试将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);
}
}
我试过了
> setOpaque(false),setBackground(null)等的各种组合.
>将SwingNode的背景设置为透明
>大量的搜索
更新
>我们找到了解决方法,请参阅答案.但是我仍然有兴趣找到一个更好的更稳定的方法来做到这一点……
解决方法:
我的同事终于找到了一些东西.系统属性swing.jlf.contentPaneTransparent,即-Dswing.jlf.contentPaneTransparent = true
结果
除此之外,我找不到任何官方文件
> RT-37810 – Swing Nodes have serious rendering artifacts when first displayed on the primary Stage提到
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.
> JavaOne “Swing 2 JavaFX” Powerpoint
标签:java,swing,javafx,javafx-8 来源: https://codeday.me/bug/20190624/1275667.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。