javafx textarea背景颜色不是css

时间:2015-04-20 10:31:33

标签: javafx textarea scenebuilder

我想在SceneBuilder中更改textarea的背景颜色。
我没有在样式菜单中更改:-fx-background-color。
因此我发现使用CSS文件更改背景颜色。

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

但我想改变除css文件之外的其他方式。 请给我一个提示。

1 个答案:

答案 0 :(得分:5)

您可以在Java代码中更改它:

@Override
public void start( Stage stage )
{
    TextArea area = new TextArea();
    Scene scene = new Scene( area, 800, 600 );
    stage.setScene( scene );
    stage.show();

    Region region = ( Region ) area.lookup( ".content" );
    region.setBackground( new Background( new BackgroundFill( Color.BROWN, CornerRadii.EMPTY, Insets.EMPTY ) ) );

    // Or you can set it by setStyle()
    region.setStyle( "-fx-background-color: yellow" );
}

为此,我们首先查找文本区域的子Region子结构,然后对其应用样式。这个动作应该在舞台出现后完成。