首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ListView中更改JavaFX字体大小

在ListView中更改JavaFX字体大小
EN

Stack Overflow用户
提问于 2016-09-21 15:19:05
回答 3查看 7.7K关注 0票数 3

我想知道如何在JavaFx中更改列表视图项文本字体大小。每一行文本的大小都会有所不同。我试过使用细胞因子属性,但我不知道如何使用它。这里有人能帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-21 15:34:00

这里也有一个类似的问题:

How to change the font size in ListView in JavaFX?

此外,您还可以使用下面的.css

代码语言:javascript
复制
.list-cell:filled:selected:focused, .list-cell:filled:selected {
    -fx-background-color: linear-gradient(#328BDB 0.0%, #207BCF 25.0%, #1973C9 75.0%, #0A65BF 100.0%);
    -fx-text-fill: white;
}

.list-cell{
    -fx-font-size:15.0;
}

.list-cell:even { /* <=== changed to even */
    -fx-background-color: white;
}

.list-cell:filled:hover {
    -fx-background-color: #0093ff;
    -fx-text-fill: white;
}

如何在JavaFX中使用外部css?

How to use external css file to style a javafx application

如何以编程方式更改文本的大小?:

先于Java8:

代码语言:javascript
复制
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> p) {
                return new ListCell<String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);

                            // decide to add a new styleClass
                            // getStyleClass().add("costume style");
                            // decide the new font size
                            setFont(Font.font(16));
                        }
                    }
                };
            }
        });

带有lambda表达式的

代码语言:javascript
复制
listView.setCellFactory(cell -> {
            return new ListCell<String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item);

                        // decide to add a new styleClass
                        // getStyleClass().add("costume style");
                        // decide the new font size
                        setFont(Font.font(16));
                    }
                }
            };
        });
票数 4
EN

Stack Overflow用户

发布于 2016-09-21 16:09:14

将文本和文本大小存储在项类中。

代码语言:javascript
复制
public class SizedText {

    private final int textSize;
    private final String text;

    public SizedText(int textSize, String text) {
        this.textSize = textSize;
        this.text = text;
    }

    public int getTextSize() {
        return textSize;
    }

    public String getText() {
        return text;
    }
}

并使用一个cellFactory,它返回根据updateItem方法中的项数据设置文本大小的ListCells。

代码语言:javascript
复制
@Override
public void start(Stage primaryStage) {
    ListView<SizedText> list = new ListView<>(FXCollections.observableArrayList(
            new SizedText(10, "10"),
            new SizedText(15, "15"),
            new SizedText(20, "20"),
            new SizedText(25, "25"),
            new SizedText(30, "30"),
            new SizedText(35, "35"),
            new SizedText(40, "40"),
            new SizedText(50, "50"),
            new SizedText(60, "60")
    ));

    list.setCellFactory(l -> new ListCell<SizedText>() {

        @Override
        protected void updateItem(SizedText item, boolean empty) {
            super.updateItem(item, empty);

            if (empty || item == null) {
                setText(null);
            } else {
                setText(item.getText());
                setFont(Font.font(item.getTextSize()));
            }
        }

    });

    Scene scene = new Scene(list);

    primaryStage.setScene(scene);
    primaryStage.show();
}
票数 1
EN

Stack Overflow用户

发布于 2016-09-21 15:35:35

在javaFx中,您可以使用css来样式不同的对象。这取决于列表视图中的对象,但是它会出现这样的情况。

代码语言:javascript
复制
ListView<Hyperlink> listview = ..

            ObservableList<Hyperlink> lists = listview.getItems();

            for(Hyperlink link:lists)
            {
                link.setStyle("-fx-background-color:#ffffff");
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39620548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档