java的textare变透明,Textarea的透明背景在JavaFX 8

本文探讨了JavaFX 8中TextArea背景透明度失效的问题,并提供了解决方案。通过修改ScrollPane及其子节点的样式,可以实现TextArea背景的透明效果。文中给出了CSS和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Since I am using JavaFX 8, all my textareas 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.

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...

Is that a known issue?

解决方案

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.

CSS Example:

.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>() {

@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)); // *

}

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值