TextArea 的 JavaFX CSS 样式不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21493634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:00:37  来源:igfitidea点击:

JavaFX CSS styling of TextArea does not work

javacssjavafx

提问by Actimia

I'm writing a simple JavaFX application, but I can't get some of the CSS styling to work. The problem is the -fx-background-colorproperty for my TextArea.

我正在编写一个简单的 JavaFX 应用程序,但我无法使用一些 CSS 样式。问题是-fx-background-color我的TextArea.

This is the relevant CSS:

这是相关的CSS:

.text-area {
  -fx-font-family: Consolas;
  -fx-highlight-fill: #00ff00;
  -fx-highlight-text-fill: #000000;
  -fx-text-fill: #00ff00;
  -fx-background-color: #000000;
}

All the fields perform as expected, except -fx-background-color, which apparently does nothing. I still have the default white background. As you can see in the picture, the TextFieldbelow, which has identical CSS, but does apply the background color as expected.

所有字段都按预期执行,除了-fx-background-color,显然什么都不做。我仍然有默认的白色背景。正如您在图片中看到的,TextField下面的具有相同的 CSS,但确实按预期应用了背景颜色。

Picture of my problem

我的问题图片

Any clues?

有什么线索吗?

回答by sandiego

Are you using scene builder?

你在使用场景构建器吗?

I tried the same css you use and everything works fine, maybe it's a bug in your version.

我尝试了您使用的相同 css,一切正常,也许这是您版本中的错误。

I tested it for text-area and text-field.

我测试了它的文本区域和文本字段。

Image

图片

回答by tonimaroni

You need to set the content:

您需要设置内容:

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

...

...

Or see this answer maybe: Transparent background of a textarea in JavaFX 8

或者看看这个答案:JavaFX 8 中文本区域的透明背景

回答by Alessandro Giusa

I had the same problem: What I did:

我遇到了同样的问题:我做了什么:

  1. Created a .css file called console.css with following content:

    .text-area {
        -fx-font-family: Consolas;
        -fx-font-size: 15;
        -fx-text-fill: #ffffff;
        -fx-display-caret:true;
    }
    
    .text-area .content {
        -fx-background-color: #000000;
    }
    
  2. On my scene called:

    scene.getStylesheets().add(this.getClass() .getResource("/stylesheets/console.css").toExternalForm());

  1. 创建了一个名为 console.css 的 .css 文件,内容如下:

    .text-area {
        -fx-font-family: Consolas;
        -fx-font-size: 15;
        -fx-text-fill: #ffffff;
        -fx-display-caret:true;
    }
    
    .text-area .content {
        -fx-background-color: #000000;
    }
    
  2. 在我的场景中调用:

    scene.getStylesheets().add(this.getClass() .getResource("/stylesheets/console.css").toExternalForm());

Explanation:
- The second part just loads the css stuff. (trivial)

- The fist part (css): You have to check which property has to be applied on which part of the object. For instance: -fx-font-family is on .text-area but -fx-background-color is on .content. Understanding this concept let you understand all of the css stuff in JavaFx.

JavaFX-CSS-Docu(recommended).

说明:
- 第二部分只是加载 css 的东西。(微不足道)

- 第一部分(css):您必须检查必须将哪个属性应用于对象的哪个部分。例如:-fx-font-family 在 .text-area 上,但 -fx-background-color 在 .content 上。理解这个概念可以让你理解 JavaFx 中的所有 css 内容。

JavaFX-CSS-Docu(推荐)。

Good programming :-)

良好的编程:-)

回答by Menai Ala Eddine

In JavaFx ,TextArea has two substuctures (Content & scrollPane) ,for each structure has all properties of TextInputControl :

在 JavaFx 中,TextArea 有两个子结构(Content 和 scrollPane),因为每个结构都具有 TextInputControl 的所有属性:

         text-area{ }
         text-area .content { }
         text-area.scroll-pane { }

回答by H. Najafi

You should use -fx-control-inner-backgroundfor example for a TextArea with id=textAreaField:

例如,您应该将-fx-control-inner-background用于 id=textAreaField 的 TextArea:

#textAreaField {
-fx-control-inner-background: #000000;
-fx-text-fill: #ffffff;}

and you can for more information, see this topic: Textarea javaFx Color

您可以了解更多信息,请参阅此主题: Textarea javaFx Color