我想知道如何在JavaFx中更改列表视图项的文本字体大小。每行文本的大小将不同。我尝试使用单元格因子属性,但不知道如何使用它。有人可以帮我吗?
这里有一个类似的问题:
此外,您可以尝试使用以下.css
进行调整:
.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?
如何以编程方式更改文本大小?:
Java8之前:
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 表达式:
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));
}
}
};
});
将文本和文本大小存储在一个项目类中
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;
}
}
@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();
}
ListView<Hyperlink> listview = ..
ObservableList<Hyperlink> lists = listview.getItems();
for(Hyperlink link:lists)
{
link.setStyle("-fx-background-color:#ffffff");
}
对于那些无法理解Fabian上面的例子的人,我认为将其分解并在更简单的情境下展示会有所帮助。它非常有效,可以让您以编程方式设置字体,而不需要使用CSS。
ListView<String> listView = new ListView<>();
listView.setCellFactory(l->new ListCell<String>() {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty || item == null) {
setText(null);
}
else {
setText(item.toString());
setFont(Fonts.Monaco(15));
}
}
});
您可以使用任何想要的类来实现此功能,只要该类具有从中获取字符串的方法,该字符串将通过setText方法显示在单元格中。例如,如果您创建自己的类,则只需编写一个覆盖toString()方法的方法,并确保该方法输出您需要在列表中显示的测试内容。在此示例中看到String的地方,如果您使用另一个类,请确保该类在这三个位置中声明。
一旦设置了单元格工厂,您可以随意添加或删除ListView中的内容,它会自动工作。
在此示例中,Fonts类是我自己创建的,它让我轻松地访问不同的字体,但您也可以轻松使用javafx.scene.text.Font.loadFont()。
因此,如果我想使用自定义类
class MyClass {
public MyClass(String text, int num) {
this.text = text;
this.num = num;
}
private final String text;
private final int num;
public String getMyText() {
return text;
}
}
class SomeClass {
public static void makeListView() {
ListView<MyClass> list = new ListView();
listView.setCellFactory(l->new ListCell<MyClass>() {
protected void updateItem(MyClass item, boolean empty) {
super.updateItem(item, empty);
if(empty || item == null) {
setText(null);
}
else {
setText(item.getMyText());
setFont(Font.loadFont("Courier New",15));
}
}
});
}
}
从那里开始,您可以将任意数量的MyClasss实例放入列表中,并使用在setFont()方法中指定的字体进行添加。