4

I'm trying to create a textfield with a title embedded in the field border like:

enter image description here

Following the solution posted here I've created a .java file called TitledBorder.java within my src>main>java directory. My FXML is in the src>main>resources directory and I've added:

<?import TitledBorder?> at the top and it shows no error like: enter image description here

I then added this code to the FXML

<HBox prefHeight="100.0" prefWidth="200.0">
    <children>
      <TitledBorder title="Email" >
        <TextField fx:id="emailField" prefHeight="44.0" prefWidth="143.0" />
      </TitledBorder>
    </children>
</HBox>

and it shows no error either. I then launch my main method which is in a class also in src>main>java but it gets an error in the .fxml saying javafx.fxml.LoadException: /C:/Users/ME/Documents/Automation/target/classes/demofxml.fxml

and

Caused by: java.lang.ClassNotFoundException
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)

I'm not sure why it references "/target/classes/..." as opposed to "/src/main/java/...".

This is the only FXML example I've found so I'm confused why I'm getting an error upon compiling, yet no errors are shown prior? Removing all reference to TitledBorder allows all my code to function/compile properly. Since its in the src package I use this code in FXML to connect w/ controller fx:controller="loadController">. CSS is added properly too.

Thoughts?

4

1 回答 1

1

线

<?import TitledBorder?>

意味着您将TitledBorder.java文件放入默认包(即此文件的源代码中没有包声明)。但是FXMLLoader,源代码检查 FXML 文件中的导入,并在下面拆分包路径名和类名loadType(...),以便稍后加载导入的类loadTypeForPackage()

private Class<?> loadType(String name, boolean cache) throws ClassNotFoundException {
    int i = name.indexOf('.');
    int n = name.length();
    while (i != -1
        && i < n
        && Character.isLowerCase(name.charAt(i + 1))) {
        i = name.indexOf('.', i + 1);
    }

    if (i == -1 || i == n) {
        throw new ClassNotFoundException();
    }

    String packageName = name.substring(0, i);
    String className = name.substring(i + 1);

    Class<?> type = loadTypeForPackage(packageName, className);

    if (cache) {
        classes.put(className, type);
    }

    return type;
}

// TODO Rename to loadType() when deprecated static version is removed
private Class<?> loadTypeForPackage(String packageName, String className) throws ClassNotFoundException {
    return getClassLoader().loadClass(packageName + "." + className.replace('.', '$'));
}

导入的类名为“TitledBorder”,因此方法i中第一行的变量loadType将被评估为,并将在下一行代码中name.indexOf('.') = -1抛出。ClassNotFoundException

通常,使用默认包是不好的做法。将 TitledBorder.java 放入某个包中并将其导入为

<?import my.some.package.TitledBorder?>
于 2016-02-06T06:28:11.157 回答