gpt4 book ai didi

java - 属性 "style"不存在或为只读

转载 作者:行者123 更新时间:2023-12-01 10:45:33 25 4
gpt4 key购买 nike

我在使用 JavaFX8 中的选择框时遇到问题。一旦我使用上面的代码,它就可以正常获取下拉列表。

<ChoiceBox fx:id="messageChoiceBox" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="24.0" xmlns:fx="http://javafx.com/fxml">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Inbox" style="-fx-background-image: url('file:resources/images/message/draft.png');" />
<String fx:value="Facebook" />
<String fx:value="Orkut" />
<String fx:value="LinkedIn" />
<String fx:value="Google Plus" />
</FXCollections>
</items>
<HBox.margin>
<Insets right="40.0" top="3.0" />
</HBox.margin>
</ChoiceBox>

但我的问题是,我还需要为每个 fx:value 使用图像

<String fx:value="Facebook" style="-fx-background-image: url('myPath/facebook.png');" />
<String fx:value="Orkut" style="-fx-background-image: url('myPath/Orkut.png');" />
<String fx:value="LinkedIn" style="-fx-background-image: url('myPath/LinkedIn.png');" />
<String fx:value="Google Plus" style="-fx-background-image: url('myPath/Google Plus.png');" />

一旦我运行这个错误..

Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "style" does not exist or is read-only. 
at javafx.fxml.FXMLLoader$Element.processValue(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(Unknown Source)...

有谁知道如何解决这个问题吗?让我知道有没有办法在FXML中的选择框中添加样式。

非常感谢大家..

最佳答案

由于 java.lang.String 不提供样式属性,因此您必须使用自己的包含值和样式的类。

请注意,ChoiceBox 不支持样式项目,因此我建议使用 ComboBox使用自定义单元工厂代替:

样式字符串类

public class StyledString {

private final String value;
private final String style;

// allows creating instances from fxml with given value and style
public StyledString(@NamedArg("value") String value, @NamedArg("style") String style) {
this.value = value;
this.style = style;
}

public String getValue() {
return value;
}

public String getStyle() {
return style;
}

}

细胞工厂

public class StyledListCellsFactory implements Callback<ListView<StyledString>, ListCell<StyledString>> {

@Override
public ListCell<StyledString> call(ListView<StyledString> param) {
return new ListCell<StyledString>() {

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

if (item == null || empty) {
setText(null);
setStyle(null);
} else {
setText(item.getValue());
setStyle(item.getStyle());
}
}

};
}

}

fxml

确保导入相关类(StyledStringStyledListCellsFactory)

<ComboBox fx:id="messageChoiceBox" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="24.0" xmlns:fx="http://javafx.com/fxml">
<cellFactory>
<StyledListCellsFactory/>
</cellFactory>
<items>
<FXCollections fx:factory="observableArrayList">
<StyledString value="Inbox" style="-fx-background-image: url('file:resources/images/ib.png');" />
<StyledString value="Facebook" style="-fx-background-image: url('file:resources/images/fb.png');" />
<StyledString value="Orkut" style="-fx-background-image: url('file:resources/images/index.jpg');" />
<StyledString value="LinkedIn" />
<StyledString value="Google Plus" />
</FXCollections>
</items>
<HBox.margin>
<Insets right="40.0" top="3.0" />
</HBox.margin>
</ComboBox>

但是,如果您始终使用图像,请考虑使用图像 url 而不是样式。您仍然可以使用 url 构造样式字符串,也可以使用单元工厂将图像显示为 graphic (有关 graphic 属性的使用示例,请参阅 ComboBox javadoc(矩形可以替换为 ImageView 并将 contentDisplay 属性设置为否则))

关于java - 属性 "style"不存在或为只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com