簡體   English   中英

屬性“樣式”不存在或為只讀

[英]Property “style” does not exist or is read-only

我在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使用image

<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不提供style屬性,因此您必須使用自己的包含值和樣式的類。

請注意, 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());
                }
            }

        };
    }

}

文件

確保相關的類已導入( 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 (請參見ComboBox javadoc以了解graphic屬性的使用示例(矩形可以替換為ImageView並設置contentDisplay屬性到別的東西))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM