JavaFX 8 中文本区域的透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21936585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent background of a textarea in JavaFX 8
提问by WarWolfen
Since I am using JavaFX 8, all my textarea
s do not apply the transparency
that has been defined in the corresponding css. It works fine in Java 7, but for the release candidate of JavaFX 8, I can't get it to behave like before.
由于我使用的是 JavaFX 8,所以我所有的textarea
s 都没有应用transparency
在相应的 css 中定义的。它在 Java 7 中运行良好,但对于 JavaFX 8 的候选版本,我无法让它像以前一样运行。
EDIT:
This question is about JavaFX TextArea, not a JTextArea.-fx-background-color: rgba(53,89,119,0.2);
does not have any effect on the textarea anymore, although it should have an alpha value of 0.2, but it is opague...
编辑:这个问题是关于 JavaFX TextArea,而不是 JTextArea。-fx-background-color: rgba(53,89,119,0.2);
不再对 textarea 产生任何影响,虽然它的 alpha 值应该为 0.2,但它是不透明的...
Is that a known issue?
这是一个已知问题吗?
回答by miho
The TextArea consists of several nodes. To make the background transparent it is necessary to change the background of the child panes as well (TextArea,ScrollPane,ViewPort,Content). This can be accomplished via CSS.
TextArea 由几个节点组成。要使背景透明,还需要更改子窗格的背景(TextArea、ScrollPane、ViewPort、Content)。这可以通过 CSS 来实现。
CSS Example:
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;
}
The same can be accomplished via code. The code shouldn't be used for production. It's just for demonstrating the node structure.
同样可以通过代码实现。该代码不应用于生产。只是为了演示节点结构。
Code Example (makes all backgrounds fully 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)); // *
}
}
});
Further reference: JavaFX CSS Documentation
进一步参考:JavaFX CSS 文档