如何多次更改TextArea背景颜色

如何多次更改TextArea背景颜色

本文介绍了如何多次更改TextArea背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JavaFX程序中有一个TextArea,我希望它能够允许用户设置其背景色.这样我就能弄清楚如何使用外部css文件更改背景颜色.

I have a TextArea in my JavaFX program that I want to be able to allow the user to set the background color of. I was able to figure out how to change the background color using an external css file by doing this.

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

但是,这只能使我具有默认设置,并且用户将无法从菜单中选择一种颜色来进行更改.

However, this would only allow me to have a default setting and the user would not be able to select a color from a menu to change it.

我也尝试过用Java代码执行此操作.

I also tried doing this in Java code.

textArea.setStyle("-fx-background-color: green");

但是它并没有改变任何东西,因为TextArea具有更深的深度.

But it doesn't change anything because the TextArea has more depth to it.

有什么方法可以多次更改背景而无需修改css文件吗?

Is there any way I can change the background multiple times without having to modify the css file?

推荐答案

使用外部CSS文件通过查找的颜色(将链接向下滚动到所有颜色样本的正下方):

Use the external CSS file to define the background color using a looked-up color (scroll down the link to just below all the color swatches):

.text-area {
    text-area-background: blue ;
}

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

(这里text-area-background本质上是您选择的任意变量名称.)

(Here text-area-background is essentially an arbitrary variable name of your choosing.)

然后,您可以以编程方式更新查找颜色的定义:

Then you can programmatically update the definition of the looked-up-color:

textArea.setStyle("text-area-background: green;");

这是SSCCE:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TextAreaColorSelection extends Application {

    @Override
    public void start(Stage primaryStage) {
        ComboBox<Color> choices = new ComboBox<>();
        choices.getItems().addAll(Color.ALICEBLUE, Color.AQUAMARINE, Color.CORNFLOWERBLUE,
                Color.ANTIQUEWHITE, Color.BLANCHEDALMOND);
        choices.setCellFactory(lv -> new ColorCell());
        choices.setButtonCell(new ColorCell());

        TextArea textArea = new TextArea();

        choices.valueProperty().addListener((obs, oldColor, newColor) -> {

            textArea.setStyle("text-area-background: "+ format(newColor) +";");

        });

        Scene scene = new Scene(new BorderPane(textArea, choices, null, null, null));
        scene.getStylesheets().add("text-area-background.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private String format(Color c) {
        int r = (int) (255 * c.getRed());
        int g = (int) (255 * c.getGreen());
        int b = (int) (255 * c.getBlue());
        return String.format("#%02x%02x%02x", r, g, b);
    }

    public static class ColorCell extends ListCell<Color> {
        private final Rectangle rect = new Rectangle(80, 20);

        public ColorCell() {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }

        @Override
        public void updateItem(Color color, boolean empty) {
            super.updateItem(color, empty);
            if (empty) {
                setGraphic(null);
            } else {
                setGraphic(rect);
                rect.setFill(color);
            }
        }
    }

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

使用CSS文件text-area-background.css:

With the css file text-area-background.css:

.text-area {
    text-area-background: white ;
}

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

这篇关于如何多次更改TextArea背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:33