由于我使用的是JavaFX 8,所以我的所有textarea都不应用在相应的css中定义的transparency。它在Java 7中运行得很好,但是对于JavaFX 8的发布候选版本,我不能让它像以前那样运行。
编辑:这个问题是关于JavaFX TextArea,而不是JTextArea。
-fx-background-color: rgba(53,89,119,0.2);对文本区域不再有任何影响,虽然它应该有一个0.2的alpha值,但它是错误的。
这是个众所周知的问题吗?
发布于 2014-02-28 23:57:13
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<Skin<?>>() {
@Override
public void changed(
ObservableValue<? extends Skin<?>> 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 Documentation
https://stackoverflow.com/questions/21936585
复制相似问题