首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在NetBeans文件中初始化对象的示例代码"Pro JavaFX 8:权威指南“中显示的FXML错误

在NetBeans文件中初始化对象的示例代码"Pro JavaFX 8:权威指南“中显示的FXML错误
EN

Stack Overflow用户
提问于 2015-05-15 05:34:53
回答 1查看 227关注 0票数 0

在"Pro JavaFX 8:构建桌面、移动和嵌入式Java客户端的最终指南“第3章中,演示如何在FXML文件中直接指定对象的示例将在NetBeans 8.0.2中生成错误(我从该书的站点下载了代码,以及迄今为止尝试过的所有其他示例)。

这个FXML片段:

代码语言:javascript
运行
复制
<discount>
        <Utilities fx:constant="TEN_PCT"/>
</discount>

应该使用在discount中定义的常量TEN_PCT初始化字段Utilities.java:

代码语言:javascript
运行
复制
package projavafx.fxmlbasicfeatures;

import java.util.ArrayList;
import java.util.List;

public class Utilities {
    public static final Double TEN_PCT = 0.1d;
    public static final Double TWENTY_PCT = 0.2d;
    public static final Double THIRTY_PCT = 0.3d;

    public static List<Integer> createList() {
        return new ArrayList<>();
    }
}

工具提示错误是“未为实用程序定义常量'TEN_PCT‘”。

同一示例中的另一个错误出现在此时:

代码语言:javascript
运行
复制
<profits>
    <HashMap q1="1000" q2="1100" q3="1200" a4="1300"/>
</profits>

如果出现错误,"Class 'java.util.HashMap‘不支持属性'q1’",其他键也有类似的错误。

造成这些错误的原因是什么,以及如何纠正它们?

下面是所有的文件(除了上面的Utilities.java ):

FXMLBasicFeatures.fxml:

代码语言:javascript
运行
复制
<?import javafx.scene.paint.Color?>
<?import projavafx.fxmlbasicfeatures.FXMLBasicFeaturesBean?>
<?import projavafx.fxmlbasicfeatures.Utilities?>
<?import java.lang.Double?>
<?import java.lang.Integer?>
<?import java.lang.Long?>
<?import java.util.HashMap?>
<?import java.lang.String?>
<FXMLBasicFeaturesBean name="John Smith"
                       flag="true"
                       count="12345"
                       xmlns:fx="http://javafx.com/fxml/1">
    <address>12345 Main St.</address>
    <foreground>#ff8800</foreground>
    <background>
        <Color red="0.0" green="1.0" blue="0.5"/>
    </background>
    <price>
        <Double fx:value="3.1415926"/>
    </price>
    <discount>
        <Utilities fx:constant="TEN_PCT"/>
    </discount>
    <sizes>
        <Utilities fx:factory="createList">
            <Integer fx:value="1"/>
            <Integer fx:value="2"/>
            <Integer fx:value="3"/>
        </Utilities>
    </sizes>
    <profits>
        <HashMap q1="1000" q2="1100" q3="1200" a4="1300"/>
    </profits>
    <fx:define>
        <Long fx:id="inv" fx:value="9765625"/>
    </fx:define>
    <inventory>
        <fx:reference source="inv"/>
    </inventory>
    <products>
        <String fx:value="widget"/>
        <String fx:value="gadget"/>
        <String fx:value="models"/>
    </products>
    <abbreviations CA="California" NY="New York" FL="Florida" MO="Missouri"/>

</FXMLBasicFeaturesBean>

FXMLBasicFeaturesBean.java:

代码语言:javascript
运行
复制
package projavafx.fxmlbasicfeatures;

import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FXMLBasicFeaturesBean {
    private String name;
    private String address;
    private boolean flag;
    private int count;
    private Color foreground;
    private Color background;
    private Double price;
    private Double discount;
    private List<Integer> sizes;
    private Map<String, Double> profits;
    private Long inventory;
    private List<String> products = new ArrayList<String>();
    private Map<String, String> abbreviations = new HashMap<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Color getForeground() {
        return foreground;
    }

    public void setForeground(Color foreground) {
        this.foreground = foreground;
    }

    public Color getBackground() {
        return background;
    }

    public void setBackground(Color background) {
        this.background = background;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    public List<Integer> getSizes() {
        return sizes;
    }

    public void setSizes(List<Integer> sizes) {
        this.sizes = sizes;
    }

    public Map<String, Double> getProfits() {
        return profits;
    }

    public void setProfits(Map<String, Double> profits) {
        this.profits = profits;
    }

    public Long getInventory() {
        return inventory;
    }

    public void setInventory(Long inventory) {
        this.inventory = inventory;
    }

    public List<String> getProducts() {
        return products;
    }

    public Map<String, String> getAbbreviations() {
        return abbreviations;
    }

    @Override
    public String toString() {
        return "FXMLBasicFeaturesBean{" +
            "name='" + name + '\'' +
            ",\n\taddress='" + address + '\'' +
            ",\n\tflag=" + flag +
            ",\n\tcount=" + count +
            ",\n\tforeground=" + foreground +
            ",\n\tbackground=" + background +
            ",\n\tprice=" + price +
            ",\n\tdiscount=" + discount +
            ",\n\tsizes=" + sizes +
            ",\n\tprofits=" + profits +
            ",\n\tinventory=" + inventory +
            ",\n\tproducts=" + products +
            ",\n\tabbreviations=" + abbreviations +
            '}';
    }
}

FXMLBasicFeaturesMain.java:

代码语言:javascript
运行
复制
package projavafx.fxmlbasicfeatures;

import javafx.fxml.FXMLLoader;

import java.io.IOException;

public class FXMLBasicFeaturesMain {
    public static void main(String[] args) throws IOException {
        FXMLBasicFeaturesBean bean = FXMLLoader.load(
            FXMLBasicFeaturesMain.class.getResource(
                "/projavafx/fxmlbasicfeatures/FXMLBasicFeatures.fxml")
        );
        System.out.println("bean = " + bean);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-15 11:15:15

您在NetBeans编辑器中看到的两个错误都是由于NetBeans本身的错误造成的。这只是NetBeans编辑器中的一个问题,在运行示例应用程序时,FXMLLoader会很高兴地处理这些语句,就像您自己注意到的那样。

第一个错误发生是因为NetBeans似乎不允许类型与封闭类类型不同的常量字段。因此,下面的代码段将工作,因为常量Double.POSITIVE_INFINITY本身是一个双倍:

代码语言:javascript
运行
复制
<discount>
    <Double fx:constant="POSITIVE_INFINITY"/>
</discount>

第二个错误发生是因为NetBeans编辑器中根本不支持映射的特殊用例。

我已经在NetBeans bugzilla中为这两种情况提交了bug报告。

Constant 'TEN_PCT未为Utilities`‘:bug.cgi?id=252416定义

Class 'java.util.HashMap' does not support property 'q1'bug.cgi?id=252417

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30252185

复制
相关文章

相似问题

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