当前位置: 首页 > 知识库问答 >
问题:

JavaFX:设置文本控件的背景色

欧阳嘉
2023-03-14

我正在使用TextFlow和一些Text项目来显示样式化的文本,但我找不到为Text项目设置简单背景颜色的方法。

我可以设置填充颜色和字体,但它没有设置背景颜色的java方法或css属性。

共有2个答案

程鸿波
2023-03-14

文本对象没有背景。您可以将其与形状(矩形、椭圆等)分组并设置该形状的颜色,也可以将对象放在堆栈窗格中并设置堆栈窗格的背景色。

邢嘉祯
2023-03-14

基于此解决方案,这是一种快速实现的方法,可使用CSS为流程窗格中的所有文本节点提供背景着色,并能够设置一系列由逗号分隔的绘制值(多达文本项)和为每个节点插入的值:

private FlowPane flow;
private Scene scene;

@Override
public void start(Stage primaryStage) {
    Text text0 = new Text("These are several ");
    Text text1 = new Text("Text Nodes ");
    Text text2 = new Text("wrapped in ");
    Text text3 = new Text("a FlowPane");
    text0.setFill(Color.WHEAT);
    text0.setFont(new Font("Times New Roman", 20));
    text1.setFill(Color.WHITE);
    text1.setFont(new Font("Verdana", 32));
    text2.setFill(Color.WHITESMOKE);
    text2.setFont(new Font("Arial", 24));
    text3.setFill(Color.WHITESMOKE);
    text3.setFont(new Font("Arial", 18));

    flow = new FlowPane(text0, text1, text2, text3);
    scene = new Scene(flow, 300, 200);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

    setBackgroundColors();
    flow.needsLayoutProperty().addListener((obs,d,d1)->setBackgroundColors());        
}

private void setBackgroundColors(){
    final Bounds out = flow.getBoundsInLocal();
    final StringBuilder sbColors = new StringBuilder();
    final StringBuilder sbInsets = new StringBuilder();
    AtomicInteger cont = new AtomicInteger();
    flow.getChildrenUnmodifiable().forEach(n->{
        sbColors.append("hsb(")
                .append((((double)cont.get())/((double)flow.getChildren().size()))*360d)
                .append(", 60%, 90%)");
        Bounds b = ((Text)n).getBoundsInParent();
        sbInsets.append(b.getMinY()).append(" ");
        sbInsets.append(Math.min(scene.getWidth(),out.getMaxX())-b.getMaxX()).append(" ");
        sbInsets.append(Math.min(scene.getHeight(),out.getMaxY())-b.getMaxY()).append(" ");
        sbInsets.append(b.getMinX());
        if(cont.getAndIncrement()<flow.getChildren().size()-1){
            sbColors.append(", ");
            sbInsets.append(", ");
        }
    });
    flow.setStyle("-fx-background-color: "+sbColors.toString()+"; -fx-background-insets: "+sbInsets.toString()+";");
}

这将导致:

调整场景大小后:

编辑

根据OP请求,使用文本流布局而不是流窗格,因为文本节点可以跨越一个文本流中的几行,所以给定的解决方案将不再有效,因为每个文本节点的边界框将与其他节点重叠。

作为一种解决方法,我们可以将文本节点拆分为单个单词文本节点,同时保持相同原始短语中的节点的相同背景色。

我将不讨论分割逻辑,但我将添加一个索引列表,其中每个索引将文本节点与其背景色索引进行映射。

private FlowPane flow;
private Scene scene;

private final List<Integer> indices=Arrays.asList(0,0,0,1,1,2,2,3,3);

@Override
public void start(Stage primaryStage) {
    List<Text> text0 = Arrays.asList(new Text("These "), new Text("are "), new Text("several "));
    List<Text> text1 = Arrays.asList(new Text("Text "), new Text("Nodes "));
    List<Text> text2 = Arrays.asList(new Text("wrapped "), new Text("in "));
    List<Text> text3 = Arrays.asList(new Text("a "), new Text("FlowPane"));
    text0.forEach(t->t.setFill(Color.WHEAT));
    text0.forEach(t->t.setFont(new Font("Times New Roman", 20)));
    text1.forEach(t->t.setFill(Color.WHITE));
    text1.forEach(t->t.setFont(new Font("Verdana", 32)));
    text2.forEach(t->t.setFill(Color.WHITESMOKE));
    text2.forEach(t->t.setFont(new Font("Arial", 24)));
    text3.forEach(t->t.setFill(Color.WHITESMOKE));
    text3.forEach(t->t.setFont(new Font("Arial", 18)));

    flow = new FlowPane();
    flow.getChildren().addAll(text0);
    flow.getChildren().addAll(text1);
    flow.getChildren().addAll(text2);
    flow.getChildren().addAll(text3);
    scene = new Scene(flow, 300, 200);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

    setBackgroundColors();
    flow.needsLayoutProperty().addListener((obs,d,d1)->setBackgroundColors());        
}

private void setBackgroundColors(){
    final Bounds out = flow.getBoundsInLocal();
    final StringBuilder sbColors = new StringBuilder();
    final StringBuilder sbInsets = new StringBuilder();
    AtomicInteger cont = new AtomicInteger();
    flow.getChildrenUnmodifiable().forEach(n->{
        sbColors.append("hsb(")
                .append((double)indices.get(cont.get())/(double)(indices.get(flow.getChildren().size()-1)+1)*360d)
                .append(", 60%, 90%)");
        Bounds b = ((Text)n).getBoundsInParent();
        sbInsets.append(b.getMinY()).append(" ");
        sbInsets.append(Math.min(scene.getWidth(),out.getMaxX())-b.getMaxX()-1).append(" ");
        sbInsets.append(Math.min(scene.getHeight(),out.getMaxY())-b.getMaxY()).append(" ");
        sbInsets.append(b.getMinX());
        if(cont.getAndIncrement()<flow.getChildren().size()-1){
            sbColors.append(", ");
            sbInsets.append(", ");
        }
    });
    flow.setStyle("-fx-background-color: "+sbColors.toString()+"; -fx-background-insets: "+sbInsets.toString()+";");
}

此流窗格现在的行为类似于文本流:

 类似资料:
  • 我在JavaFx应用程序中有一个文本流小部件,我需要为其更改背景颜色。 布局使用FXML构建,文本流的背景颜色需要使用外部css文件设置。 我能够设置文本区域和文本字段的背景颜色,但无法为文本流这样做。

  • 看,应用程序的背景颜色和2个禁用文本字段的背景颜色不同问题:如何更改禁用和不可编辑文本字段的背景颜色。 是右边的文本字段(在照片中)。我尝试过的是:把

  • 当批准日期晚于今天时,我试图更改行的颜色。此时,行应该被涂成红色……知道为什么这段代码不能工作吗?

  • 我想粘贴一个相对图像url到一个div设置为背景图像。不幸的是,div不会呈现图像。因此,这工作良好,并呈现图像 但这个不是 我也尝试过的事情: 在单引号中包装url 可能吗? 从src文件夹开始 和从“资源”文件夹开始 背景图像的正确url是什么? 更新 我在用VueJs所以这里的情况可能会有所不同?再现的步骤: 使用Vue CLI创建新项目 在中创建 目录 在中创建映像,并将其称为 使用更新a

  • 问题内容: 我正在使用Nimbus外观。我需要在JTabbedPane中更改选项卡的背景色和前景色,但在JTabbedPane中未设置颜色。我尝试了setForeground(),setForegroundAt(),setBackground()和setBackgroundAt()方法,但没有用。这是我的代码 } 问题答案: 您可以执行几项不同的操作,具体取决于您希望对确切颜色进行多少控制。最简单

  • 通过RGB值设置背景的颜色。 默认的颜色是 0x000000: // 颜色的参数可以是字符串 "#530000" 或者是十六进制数值 0x530000 controller.setBackgroundColor("#530000); //controller.setBackgroundColor(0x530000);