由于我使用的是 JavaFX 8,所以我的所有 textarea
都不会应用相应 css 中定义的 transparency
。它在 Java 7 中运行良好,但对于 JavaFX 8 的候选版本,我无法让它像以前一样运行。
编辑:
这个问题是关于JavaFX TextArea,而不是JTextArea。
-fx-background-color: rgba(53,89,119,0.2);
对文本区域不再有任何影响,虽然它的 alpha 值为 0.2,但它是不透明的...
这是一个已知问题吗?
最佳答案
TextArea 由多个节点组成。要使背景透明,还需要更改子 Pane 的背景(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
关于java - JavaFX 8 中文本区域的透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783446/