【问题标题】:Textarea javaFx Color文本区域 javaFx 颜色
【发布时间】:2016-07-25 04:58:48
【问题描述】:

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

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

我知道我的问题在这里看起来像是重复的:

javafx textarea background color not css

JavaFX CSS styling of TextArea does not work

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

到目前为止我尝试了什么(不成功):

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

在源代码中:

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

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

【问题讨论】:

  • 为什么要在没有 CSS 的情况下这样做?这是更改控件样式的受支持机制。

标签: java javafx textarea


【解决方案1】:

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

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

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

在您的 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; ");

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

SSCCE:

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>

还有一个测试类:

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);
    }
}

【讨论】:

  • -fx-control-inner-background: #000000 设置文本区域的背景颜色,仅供其他可能尝试过的人参考-fx-background-color
【解决方案2】:

很简单,只要在你想要的元素里面放一个id名字就好了:

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

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

【讨论】:

    【解决方案3】:

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

    代码:

    #tContent{
        -fx-pref-width: 180;
        -fx-pref-height: 110;
    
    }
    .content {
        -fx-background-color: transparent;
    }
    .viewport{
        -fx-background-color: transparent; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      相关资源
      最近更新 更多