JavaFX - 为不同TextArea的内容设置不同的背景

时间:2016-10-14 21:40:20

标签: java css javafx javafx-2 javafx-8

我是JavaFX的新手。如何为不同TextArea的内容设置不同的背景颜色。据我所知,使用CSS,我可以设置背景颜色,如

.text-area {
    -fx-background-color: transparent;
    -fx-text-box-border: gray;
}

.text-area .scroll-pane .content{
    -fx-background-color: transparent;
}

但它影响了TextArea两个。

JavaFX中禁用的TextArea的背景颜色是什么?如何修改它?

TextArea textarea = new TextArea();
TextArea textarea1 = new TextArea();

这些是我申请的属性

 textarea1.setMaxHeight(180);
 textarea1.setMaxWidth(500);
 textarea.setEditable(false);
 textarea.setPrefRowCount(15);
 textarea.setWrapText(true);
 textarea.setStyle("-fx-background-color: transparent");
 textarea1.setStyle("-fx-background-color: tomato");

2 个答案:

答案 0 :(得分:2)

您可以在CSS中引入自定义变量来确定颜色。

禁用TextArea时,TextArea和子项的不透明度设置为0.4(= 40%)。如果愿意,可以通过覆盖样式表中的属性来撤消此操作。

.text-area {
    /* use variable as inner background */
    -fx-control-inner-background: content-background;
}

/* keep element fully opaque, when disabled */
.text-area:disabled,
.text-area *:disabled {
    -fx-opacity: 1;
}

/* replace inner background with darker color, when disabled */
.text-area:disabled {
    -fx-control-inner-background: derive(content-background, -40%);
}
// set content-background from inline style
textarea.setStyle("content-background: transparent;");
textarea1.setStyle("content-background: tomato;");

如果您不需要颜色来根据您选择的颜色(-fx-control-inner-background部分)确定derive,您也可以简单地从内联样式指定属性。在这种情况下,您不需要样式表中背景的CSS规则。

textarea.setStyle("-fx-control-inner-background: transparent;");
textarea1.setStyle("-fx-control-inner-background: tomato;");

答案 1 :(得分:1)

所以你需要做的就是把这一行放在你的css页面中:

.text-area .content {
    -fx-background-color: text-area-background ;
}

所以现在无论你将文本区域背景设置为它,都会将内容设置回来。所以你应该能够在下面这样做,它会起作用:

TextArea one = new TextArea();
        TextArea two = new TextArea();
        TextArea three = new TextArea();

        one.setStyle("-fx-background-color: transparent");
        two.setStyle("-fx-background-color: tomato");
        three.setStyle("-fx-background-color: steelblue");