java - 我需要使用资源包的帮助,我可以在其中更改我的 java 程序中的语言

标签 java swing internationalization

问题: 在Eclipse中创建一个新项目
修改上一张幻灯片中的 JFrame。添加一个 JComboBox,其中列出了“English”和“French”。当选择法语选项时,使用资源包将英语 JButton 翻译为法语“Montrer tous les Locales”。当选择法语时,JComboBox 还应该国际化以读取“Francais”和“Anglais”。

代码:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel; 
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Exercise2 extends JFrame implements ActionListener {
private ResourceBundle res;

private static final long serialVersionUID = 1L;
JButton button;
JFrame frame;
JTextArea ta;
JLabel label;
JComboBox<String> combo;
String[] array;

Locale[] availableLocales = Calendar.getAvailableLocales();

public Exercise2() {
    Locale loc = new Locale("de","DE"); //create Locale for German in Germany

    getRes();
    setRes(ResourceBundle.getBundle("ProgramResource",loc)); //Create resource bundle

    JLabel greetLabel = new JLabel(getRes().getString("greeting")); //get value for greeting key value

    JButton computeButton = new JButton(getRes().getString("computeButton")); //button key value

    getContentPane().add(greetLabel);
    getContentPane().add(computeButton,BorderLayout.SOUTH);

    Container c = getContentPane();
    JPanel p = new JPanel();
    String[] array = { "English", "French" };
    combo = new JComboBox<String>(array);

    label = new JLabel(getRes().getString("greeting"));
    button = new JButton(getRes().getString("Button"));

    ta = new JTextArea(10,12);
    ta.setEditable(false);

    button = new JButton();
    button.setText("List all Locales");
    button.addActionListener(this);

    JScrollPane output = new JScrollPane(combo);

    p.add(button, BorderLayout.NORTH);
    p.add(output);
    p.add(combo);
    c.add(p);

    setSize(300, 300);
    setVisible(true);
}

public void actionPerformed(ActionEvent a) {
    // TO DO Auto-generated method stub
    if (a.getActionCommand().equals("List all Locales")) {

        for (int i = 0; i < availableLocales.length; i++) {

            ta.append(availableLocales[i].getDisplayName() + "\n");
        }
    }
}

public static void main(String[] args) {
    Exercise2 myLocaleTest = new Exercise2();
    myLocaleTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public ResourceBundle getRes() {
    return res;
}

public void setRes(ResourceBundle res) {
    this.res = res;
}
}

所以我不断收到错误消息:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ProgramResource, locale de_DE
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:795)
    at Exercise2.<init>(Exercise2.java:42)
    at Exercise2.main(Exercise2.java:89)

我不知道这意味着什么,每次我更改某些内容时,它似乎都不起作用

最佳答案

您必须创建一个文本文件,其中包含诸如eg.button=france 之类的文本和ResourceBundle 类,如下所示:

import java.io.IOException;
import java.util.PropertyResourceBundle;


public class ProgramProperties_de extends PropertyResourceBundle {

  public ProgramProperties_de() throws IOException {
    super(ProgramProperties_de.class.getResourceAsStream("ProgramProperties_de.txt"));
  }
}

关于java - 我需要使用资源包的帮助,我可以在其中更改我的 java 程序中的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200815/

相关文章:

java - hibernate vs hibernate-core maven 包

java - Android 明文

java - Android Studio ListView 复选框保存

java - 在 Intellij 的表单中制作一个 hello world 应用程序

java - 如何编辑 F2 的 JTable 键绑定(bind)

android-studio - Android Studio 国际化 - 对 XLIFF 标准的 IDE 支持

c# - 浏览器中的全局化、缓存控制和 HTTP 日期

documentation - 如何提供其他语言的javadoc?

java - java中绘制图像时jFrame GetGraphics为空

java - 你如何在 Java 中制作如此复杂的 GUI? Gwt有可能吗?还是需要用别的东西?