gpt4 book ai didi

java - Java 中的国际化错误

转载 作者:行者123 更新时间:2023-12-01 11:51:23 26 4
gpt4 key购买 nike

我正在学习Java,我们的老师告诉我们一个netbeans中的国际化示例,他为不同的语言创建了三个属性文件(在netbeans中你可以轻松创建属性文件),所以我在家里使用eclipse尝试这个,现在我创建了3 个属性文件与我们老师创建的文件夹完全相同(.java 文件所在的文件夹)我做了与他相同的代码,但我得到了一个错误,然后我尝试在netbeans中运行netbeans项目(我从老师的计算机上获取它),它给了我同样的错误,现在我认为编码很好,但是有属性文件中存在一些错误

这是代码

package example;


import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;


public class MyFrame extends JFrame {

private static final long serialVersionUID = 1L;
/**
*
*/
private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyFrame frame = new MyFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 156);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

final JLabel lbl = new JLabel("My Internationalization Example");
lbl.setFont(new Font("Tahoma", Font.PLAIN, 14));
lbl.setBounds(106, 21, 204, 17);
contentPane.add(lbl);

JRadioButton rdbtnEnglish = new JRadioButton("English");
rdbtnEnglish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

Locale lang = new Locale("en" , "US");
ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_en_US", lang);
lbl.setText(message.getString("LangValue"));
}
});
rdbtnEnglish.setBounds(42, 59, 109, 23);
contentPane.add(rdbtnEnglish);

JRadioButton rdbtnFrance = new JRadioButton("France");
rdbtnFrance.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

Locale lang = new Locale("fr" , "FR");
ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_fr_FR", lang);
lbl.setText(message.getString("LangValue"));
}
});
rdbtnFrance.setBounds(153, 59, 142, 23);
contentPane.add(rdbtnFrance);

JRadioButton rdbtnGerman = new JRadioButton("German");
rdbtnGerman.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

Locale lang = new Locale("de" , "DE");
ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_de_DE", lang);
lbl.setText(message.getString("LangValue"));
}
});
rdbtnGerman.setBounds(297, 59, 109, 23);
contentPane.add(rdbtnGerman);


ButtonGroup myGroup = new ButtonGroup();
myGroup.add(rdbtnEnglish);
myGroup.add(rdbtnFrance);
myGroup.add(rdbtnGerman);

rdbtnEnglish.setSelected(true);




}

}

这是所有三个属性文件中的内容

MessageBundle_en_US.properties

LangValue = My Internationalization Example

MessageBundle_de_DE.properties

LangValue = Meine Internationalisierung Beispiel

MessageBundle_fr_FR.properties

LangValue = Mon internationalisation Exemple

现在这是我的属性和java文件所在的文件夹

D:\Documents\Work\Eclipse\InternationalizationExample\src\example

这是一个屏幕截图,可以更好地查看

enter image description here

enter image description here

现在,当我运行时,我的程序会运行,但是当我选择选项时,它会给我这个错误,我在谷歌上搜索了整整3个小时,并尝试了从更改位置、文件夹等所有内容

enter image description here

当我单击另一个选项 BOOM 时,出现错误

enter image description here

错误是

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name src/example/MessageBundle_fr_FR, locale fr_FR at java.util.ResourceBundle.throwMissingResourceException(Unknown Source) at java.util.ResourceBundle.getBundleImpl(Unknown Source) at java.util.ResourceBundle.getBundle(Unknown Source) at example.MyFrame$3.actionPerformed(MyFrame.java:75) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

为了获得更多帮助,我已将整个 Eclipse 项目上传到此 rar 中

http://www.mediafire.com/download/yxnsrch41qhcxuw/InternationalizationExample.rar

最佳答案

将 getBundle 语句更改为:

ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_en_US", lang);

至:

ResourceBundle message = ResourceBundle.getBundle("example/MessageBundle", lang);

关于java - Java 中的国际化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28800421/

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