如何多次更改 TextArea 背景颜色?

问题描述 投票:0回答:2

我的 JavaFX 程序中有一个

TextArea
,我希望能够允许用户设置其背景颜色。通过这样做,我能够弄清楚如何使用外部
css
文件更改背景颜色。

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

但是,这只会让我有一个默认设置,用户将无法从菜单中选择颜色来更改它。

我也试过用 Java 代码这样做。

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

但它不会改变任何东西,因为

TextArea
有更多的深度。

有什么方法可以多次更改背景而无需修改

css
文件?

javafx-8 javafx-css
2个回答
10
投票

使用外部 CSS 文件使用 查找颜色 定义背景颜色(向下滚动链接到所有颜色样本下方):

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

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

(这里

text-area-background
本质上是您选择的任意变量名称。)

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

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:

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

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

1
投票

我用过这样的东西

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

我觉得快一点

© www.soinside.com 2019 - 2024. All rights reserved.