java 文本区域 javaFx 颜色

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

Textarea javaFx Color

javajavafxtextarea

提问by ΦXoc? ? Пepeúpa ツ

Am trying to develop an app that looks like a terminal console, am using a TextArea for that and my wish is to habe a black background and green text,

我正在尝试开发一个看起来像终端控制台的应用程序,为此我使用了 TextArea,我的愿望是拥有黑色背景和绿色文本,

the point i want to do this without using any ccs template

我想在不使用任何 ccs 模板的情况下做到这一点

I know that my question can look like a duplicated in here:

我知道我的问题可能在这里重复:

javafx textarea background color not css

javafx textarea背景颜色不是css

or

或者

JavaFX CSS styling of TextArea does not work

TextArea 的 JavaFX CSS 样式不起作用

but after reading those and trying what they suggested I found no luck to solve my issue

但是在阅读了这些并尝试了他们的建议后,我发现没有运气来解决我的问题

What I tried so far:

到目前为止我尝试过的:

in the FXML:

在 FXML 中:

<TextArea 
    fx:id="terminalTextArea"
    layoutX="14.0"
    layoutY="85.0"
    prefHeight="64.0"
    prefWidth="402.0"
    style="-fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; -fx-background-color:#000000;"
    text="Terminal"
    AnchorPane.leftAnchor="10.0"
    AnchorPane.rightAnchor="10.0">
    <font>
        <Font name="System Bold" size="14.0" />
    </font>
</TextArea>

but no luck ....

但没有运气....

and in the source-code:

并在源代码中:

@Override
public void initialize(URL url, ResourceBundle rb) {
    System.out.println("init here");
    terminalTextArea.setStyle("-fx-text-fill: black;");
}

the only thing that I get is a bordered color like in the image below....

我唯一得到的是一个带边框的颜色,如下图所示......

does any one of you had the same problem before????

你们中有人以前遇到过同样的问题吗????

Tnxs in advance

提前交易

enter image description here

在此处输入图片说明

回答by James_D

The recommended way is to use an external CSS file, as in the examples you linked.

推荐的方法是使用外部 CSS 文件,如您链接的示例中所示。

If for some reason you don't want to do that, try

如果由于某种原因您不想这样做,请尝试

style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "

in your FXML file, or equivalently

在您的 FXML 文件中,或等效地

terminalTextArea.setStyle("-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; ");

in your controller's initialize()method.

在您的控制器initialize()方法中。

SSCCE:

SSCCE:

StyledTextArea.fxml:

StyledTextArea.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.text.Font?>

<HBox xmlns:fx="http://javafx.com/fxml/1" >
    <padding>
        <Insets top="12" left="12" right="12" bottom="12"/>
    </padding>
    <TextArea 
        prefHeight="64.0"
        prefWidth="402.0"
        style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "
        text="Terminal">

        <font>
            <Font name="System Bold" size="14.0" />
        </font>
    </TextArea>
</HBox>

and a test class:

和一个测试类:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class StyledTextArea extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(
            FXMLLoader.load(getClass().getResource("StyledTextArea.fxml"))
        ));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

在此处输入图片说明

回答by Andrés Rodríguez

It's simple, just put an id name inside the elment you want like:

很简单,只要在你想要的 elment 里面放一个 id 名称,就像:

AnchorPane id="AnchorPane" prefHeight="180.0" prefWidth="430.0"

or in Scene Builder in properties just down below the title JavaFX CSS write the name and style this element in your CSS file.

或在Scene Builder 中,在JavaFX CSS 标题下方的属性中,在CSS 文件中写入此元素的名称和样式。

回答by Maker

if you are trying to set the background of textArea using CSS, you should have this attributes of CSS .content . viewport //additional .scroll-pane

如果您尝试使用 CSS 设置 textArea 的背景,您应该拥有 CSS .content 的这个属性。视口//additional .scroll-pane

Codes:

代码:

#tContent{
    -fx-pref-width: 180;
    -fx-pref-height: 110;

}
.content {
    -fx-background-color: transparent;
}
.viewport{
    -fx-background-color: transparent; 
}