gpt4 book ai didi

java - 如何使用 ResourceBundle

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:43 30 4
gpt4 key购买 nike

我正试图掌握 Java 应用程序的内部化,如图所示 here .我不能。我创建了一个扩展 ListResourceBundle 的类,并尝试检索 key 。不过,我一直有异常(exception)。如果您查看教程,它说要使用 .class 文件。这不可能是对的,对吗?

[项目树]

enter image description here

[源代码]

这是我正在使用的两个类,只是 MainWindow_xx_XX.java 文件中的一个,因为它们基本相同。首先是 ListResourceBundle:

public class MainWindow_en_US extends ListResourceBundle {

@Override
protected Object[][] getContents() {
return contents;
}
private Object[][] contents = {
{"fileLabel", "File"},
{"newSessionLabel", "New session..."},
{"openSessionLabel", "Open session..."},
{"saveLabel", "Save"},
{"exitLabel", "Exit"},
{"editLabel", "Edit"},
{"toolsLabel", "Tools"},
{"helpLabel", "Help"}
};
}

现在是我用来加载它的方法:

    private static final int DEFAULT_LOCALE = 0;
private ResourceBundle bundle;
public static Locale locale;
public static final Locale[] supportedLocales = {
new Locale("en", "US"),
new Locale("es", "ES")
};

public MainWindow() {
for (Locale i : supportedLocales) {
if (i.getLanguage().equals(Locale.getDefault().getLanguage())) {
locale = i;
break;
} else {
locale = supportedLocales[DEFAULT_LOCALE];
}
}
bundle = ResourceBundle.getBundle("MainWindow", locale); //EXCEPTION POINTS HERE!!!
initComponents();
}

我不断收到以下异常。我知道我可以通过属性文件来完成它,但它让我无法理解,因为我无法使用 Oracle 极其简单的教程。

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name MainWindow, locale es_ES
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:796)
at -.-.-.-.-.<init>(MainWindow.java:37)
at -.-.-.-.MainWindow$2.run(MainWindow.java:145)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

最佳答案

您需要使用完全限定的基本名称:

bundle = ResourceBundle.getBundle("pkg.subpkg.resources.MainWindow", locale);

关于java - 如何使用 ResourceBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14464153/

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