gpt4 book ai didi

javafx i18n : "No resources specified" exception

转载 作者:行者123 更新时间:2023-11-30 02:03:23 31 4
gpt4 key购买 nike

我正在为我的 javafx 应用程序学习 i18n。

文件:

java/
--LoginScreen.java
resources/
--login.fxml
--login_en.properties
--login_ru.properties

登录屏幕.java

@Override
public void start(Stage primaryStage) throws Exception
{
URL url = getClass().getClassLoader().getResource("login.fxml");

if (url == null)
{
System.err.println("Unable to load UI file.");
return;
}

FXMLLoader loader = new FXMLLoader();
String localeStr = "en"; //TODO: actual language selection
loader.setResources(ResourceBundle.getBundle("login", new Locale(localeStr)));
Parent root = loader.load(url);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

登录.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.layout.VBox?>

<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.LoginScreen">
<Label text="%label_User" />
<TextField fx:id="textField_user" />
<Label text="%label_Password" />
<PasswordField fx:id="passwordField_password" />
</VBox>

login_en.preperties:

label_User=User:
label_Password=Password:

login_ru.properties:

label_User=Пользователь:
label_Password=Пароль:

我收到以下异常,然后它尝试执行Parent root = loader.load(url)

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: No resources specified.
/Users/user/Documents/javafx/build/resources/main/login.fxml:9

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.resolvePrefixedValue(FXMLLoader.java:421)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at app.LoginScreen.start(LoginScreen.java:52)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application app.LoginScreen

我似乎无法从资源中获取本地化字符串,但是当我调试它时,我肯定可以看到加载器正确加载资源。

我错过了什么,因为我自己无法确定错误。

最佳答案

通过将 URL 传递给 load 方法,您将调用一个 static load 方法,该方法不会无权访问您分配给 FXMLLoader 实例属性的任何数据。声明

Parent root = loader.load(url);

Parent root = FXMLLoader.load(url);

编译为相同的字节码。

通过将 URL 作为构造函数参数传递或使用 setLocation 方法来传递 URL,并调用无参数 load 方法:

FXMLLoader loader = new FXMLLoader(url);
String localeStr = "en"; //TODO: actual language selection
loader.setResources(ResourceBundle.getBundle("login", new Locale(localeStr)));
Parent root = loader.load();

您也可以通过将两个参数传递给 static load 方法来完成此操作,而无需自行创建 FXMLLoader 实例:

String localeStr = "en";
Parent root = loader.load(url, ResourceBundle.getBundle("login", new Locale(localeStr)));

关于javafx i18n : "No resources specified" exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52099392/

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