Textarea的透明背景在JavaFX 8

由于我使用的是JavaFX 8,因此我的所有textarea都不应用相应css中定义的transparency 。 它在Java 7中运行良好,但对于JavaFX 8的候选版本,我无法像以前那样表现。

编辑:这个问题是关于JavaFX TextArea,而不是JTextArea。
-fx-background-color: rgba(53,89,119,0.2); 对textarea没有任何影响,虽然它应该有一个0.2的alpha值,但它是不相干的……

这是一个已知问题吗?

TextArea由几个节点组成。 要使背景透明,还需要更改子窗格的背景(TextArea,ScrollPane,ViewPort,Content)。 这可以通过CSS完成。

CSS示例:

 .text-area { -fx-background-color: rgba(53,89,119,0.4); } .text-area .scroll-pane { -fx-background-color: transparent; } .text-area .scroll-pane .viewport{ -fx-background-color: transparent; } .text-area .scroll-pane .content{ -fx-background-color: transparent; } 

通过代码可以实现同样的目的。 代码不应用于生产。 它只是用于演示节点结构。

代码示例(使所有背景完全透明):

  TextArea textArea = new TextArea("I have an ugly white background :-("); // we don't use lambdas to create the change listener since we use // the instance twice via 'this' (see *) textArea.skinProperty().addListener(new ChangeListener>() { @Override public void changed( ObservableValue> ov, Skin t, Skin t1) { if (t1 != null && t1.getNode() instanceof Region) { Region r = (Region) t1.getNode(); r.setBackground(Background.EMPTY); r.getChildrenUnmodifiable().stream(). filter(n -> n instanceof Region). map(n -> (Region) n). forEach(n -> n.setBackground(Background.EMPTY)); r.getChildrenUnmodifiable().stream(). filter(n -> n instanceof Control). map(n -> (Control) n). forEach(c -> c.skinProperty().addListener(this)); // * } } }); 

进一步参考: JavaFX CSS文档