我的JavaFX应用程序具有fx:id为的Label location。它在FXML文件中定义。当我尝试运行该应用程序时,出现以下错误:
java.lang.IllegalArgumentException:无法将javafx.scene.control.Label字段sample.Controller.location设置为java.net.URL
我正在将JDK 12与JavaFX 11.0.2一起使用。
我在SO上看到了其他答案,说这是由locationLabel 类型冲突引起的。例如,它可以在FXML文件中声明为Label,但在Java代码中则是别的(在本例中为java.net.URL)。但是,正如您在下面的代码中看到的那样,我没有在任何地方使用URL类。
将fx:id更改为其他名称(例如loc)会使错误消失,因此location必须是一个“魔术”名称。
是什么原因造成的?
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label fx:id="location" layoutX="133.0" layoutY="146.0" text="Output" />
</children>
</Pane>
Run Code Online (Sandbox Code Playgroud)
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 275));
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller
{
@FXML
Label location;
}
Run Code Online (Sandbox Code Playgroud)
module MyTest
{
requires javafx.controls;
requires javafx.fxml;
opens sample;
}
Run Code Online (Sandbox Code Playgroud)
通过检查比较容易找到FXMLLoader:
/**
* A key for location URL in namespace map.
* @see #getNamespace()
* @since JavaFX 2.2
*/
public static final String LOCATION_KEY = "location";
/**
* A key for ResourceBundle in namespace map.
* @see #getNamespace()
* @since JavaFX 2.2
*/
public static final String RESOURCES_KEY = "resources";
private URL location;
private ResourceBundle resources;
private <T> T loadImpl(InputStream inputStream,
Class<?> callerClass) throws IOException {
...
namespace.put(LOCATION_KEY, location);
namespace.put(RESOURCES_KEY, resources);
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,这意味着location(例如resources,controller和Controller)是保留关键字。
这些关键字被添加到namespace(可观察的地图 :)中ObservableMap<String, Object> namespace,并且location是FXML文件的URL,例如:
namespace.put("location", "file:/path/to/your/FXML/file");
Run Code Online (Sandbox Code Playgroud)
稍后,将fx:id评估FXML文件的不同标签并将其添加到同一名称空间:
private void processValue() throws LoadException {
...
if (fx_id != null) {
namespace.put(fx_id, value);
injectFields(fx_id, value);
}
...
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果您fx:id是"location",则将执行以下操作:
namespace.put("location", Label@12dafafa);
Run Code Online (Sandbox Code Playgroud)
显然,由于您使用的是相同的密钥,因此您要用一个新对象(在本例中为Label)覆盖以前的URL。
稍后,当FXMLLoader尝试注入此位置时:
injectFields(LOCATION_KEY, location);
Run Code Online (Sandbox Code Playgroud)
发生异常,因为Label可以将映射从键获取的"location"设置为URL字段:
Can not set javafx.scene.control.Label field org.openjfx.FXMLController.location to java.net.URL
Run Code Online (Sandbox Code Playgroud)
尽管没有记录,但这意味着您不能使用"location"(nor "resources","controller")作为fx:id标记的值,因为它将与这些关键字发生冲突。