Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用资源包更改标签_Java_Menu_Internationalization_Locale_Resourcebundle - Fatal编程技术网

Java 使用资源包更改标签

Java 使用资源包更改标签,java,menu,internationalization,locale,resourcebundle,Java,Menu,Internationalization,Locale,Resourcebundle,我正在编写一个Java应用程序,我只是停留在一个点上。我想用菜单项语言制作一个菜单,通过点击所需的语言,它显然会改变应用程序的语言。为此,我编写了两个.properties文件:一个默认名为LabelsBundle(英语),一个扩展名为LabelsBundle_de(德语)。我有这样的课: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import jav

我正在编写一个Java应用程序,我只是停留在一个点上。我想用菜单项语言制作一个菜单,通过点击所需的语言,它显然会改变应用程序的语言。为此,我编写了两个.properties文件:一个默认名为LabelsBundle(英语),一个扩展名为LabelsBundle_de(德语)。我有这样的课:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class MenuBar extends JFrame implements ActionListener 
{
private JMenuBar menuBar;
private JMenu language, about;
private JMenuItem UKLang, DELang, FRLang, ESPLang, POLLang;
private ResourceBundle labels;
private Locale currentLocale;

public  MenuBar()
{
    menuBar = new JMenuBar();
    language = new JMenu("Language");
    about = new JMenu("About");

    UKLang = new JMenuItem("English");
    DELang = new JMenuItem("German");
    FRLang = new JMenuItem("French");
    //ESPLang = new JMenuItem("Spanish");
    POLLang = new JMenuItem("Polish");

    UKLang.addActionListener(this);
    DELang.addActionListener(this);

    language.add(UKLang);
    language.add(DELang);
    language.add(FRLang);
    language.add(POLLang);

    menuBar.add(Box.createVerticalGlue());
    menuBar.add(language);
    menuBar.add(about);

}

public JMenuBar getMenu()
{
    return menuBar;
}

@Override
public void actionPerformed(ActionEvent e) 
{
    Object input = e.getSource();


    if(input == UKLang)
    {
        currentLocale = new Locale("en");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == DELang)
    {
        currentLocale = new Locale("de");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == FRLang)
    {
        currentLocale = new Locale("fr");
        labels = ResourceBundle.getBundle("LabelsBundle_fr", currentLocale);
    }

    else if(input == POLLang)
    {
        currentLocale = new Locale("pl");
        labels = ResourceBundle.getBundle("LabelsBundle_pol", currentLocale);
    }

}

}
# This is the default LabelsBundle.properties file

amplitude_label = Amplitude
frequency_label = Frequency (Hz)
phase_label = Phase (Degrees)
component_waves_label = Component Waves
result_wave_label = Result Wave
play_button = Play
play_triade_button = Play Triad
save_image_button = Save Image
save_sample_button = Save Sample
save_success_message = File saved successfully
save_error_message = File could not be saved
wave_1 = Wave 1
wave_2 = Wave 2
wave_3 = Wave 3
about = test
我不知道下一步该怎么办。我的朋友告诉我

object.setText(resourceBundle.getString("blabla"));
但是我真的不知道我怎么能把它用在像这样的东西上

resultWaveLabel = new JLabel("Result wave:");
我想我需要写一些像

resultWaveLabel.setText(...);
我说得对吗

顺便说一句,当我在菜单列表中单击德语时,我收到一个错误

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name LabelsBundle, locale de

编辑:我想强调的是,主要问题是如何更改上述内容的标签,而不是消除上述错误。这已经解决了。

异常

此输出是预期的,因为您的项目或应用程序中没有名为“LabelsBundle”的资源文件

如果资源文件存在,但所需资源不存在,也会发生同样的情况

另外,将这个名为LabelsBundle.properties的资源文件以及您想要的任何资源放在与
MenuBar
类相同的目录中。表示java文件和属性文件应位于同一目录中

如果这个java文件和属性文件在一个包中,您必须给出加载它的完全限定路径

例如,如果属性文件位于一个名为newpackage的包中,那么您应该像这样加载资源

ResourceBundle.getBundle("newpackage.LabelsBundle", currentLocale);
设置标签的值

参考这个,它是更多的细节 将ResourceBundle分配给变量(您的案例it
labels
)后,只需将

更准确地说

@Override
public void actionPerformed(ActionEvent e) 
{
    Object input = e.getSource();


    if(input == UKLang)
    {
        currentLocale = new Locale("en");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == DELang)
    {
        currentLocale = new Locale("de");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == FRLang)
    {
        currentLocale = new Locale("fr");
        labels = ResourceBundle.getBundle("LabelsBundle_fr", currentLocale);
    }

    else if(input == POLLang)
    {
        currentLocale = new Locale("pl");
        labels = ResourceBundle.getBundle("LabelsBundle_pol", currentLocale);
    }
     //Here you go. the magical code to do that
     resultWaveLabel.setText(labels.getString("blabla"));///Here that's it your label is updated with the text 
}

在Madhan的帮助下,作为对我自己问题的回答,我的班级现在看起来是这样的(听起来是另一个班级的名字!):

我的一个捆绑资源文件如下所示:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class MenuBar extends JFrame implements ActionListener 
{
private JMenuBar menuBar;
private JMenu language, about;
private JMenuItem UKLang, DELang, FRLang, ESPLang, POLLang;
private ResourceBundle labels;
private Locale currentLocale;

public  MenuBar()
{
    menuBar = new JMenuBar();
    language = new JMenu("Language");
    about = new JMenu("About");

    UKLang = new JMenuItem("English");
    DELang = new JMenuItem("German");
    FRLang = new JMenuItem("French");
    //ESPLang = new JMenuItem("Spanish");
    POLLang = new JMenuItem("Polish");

    UKLang.addActionListener(this);
    DELang.addActionListener(this);

    language.add(UKLang);
    language.add(DELang);
    language.add(FRLang);
    language.add(POLLang);

    menuBar.add(Box.createVerticalGlue());
    menuBar.add(language);
    menuBar.add(about);

}

public JMenuBar getMenu()
{
    return menuBar;
}

@Override
public void actionPerformed(ActionEvent e) 
{
    Object input = e.getSource();


    if(input == UKLang)
    {
        currentLocale = new Locale("en");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == DELang)
    {
        currentLocale = new Locale("de");
        labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
    }

    else if(input == FRLang)
    {
        currentLocale = new Locale("fr");
        labels = ResourceBundle.getBundle("LabelsBundle_fr", currentLocale);
    }

    else if(input == POLLang)
    {
        currentLocale = new Locale("pl");
        labels = ResourceBundle.getBundle("LabelsBundle_pol", currentLocale);
    }

}

}
# This is the default LabelsBundle.properties file

amplitude_label = Amplitude
frequency_label = Frequency (Hz)
phase_label = Phase (Degrees)
component_waves_label = Component Waves
result_wave_label = Result Wave
play_button = Play
play_triade_button = Play Triad
save_image_button = Save Image
save_sample_button = Save Sample
save_success_message = File saved successfully
save_error_message = File could not be saved
wave_1 = Wave 1
wave_2 = Wave 2
wave_3 = Wave 3
about = test
为了实现这一点,我必须使其他类中的所有对象都是静态的:

public static JLabel amplitudeLabel, frequencyLabel, phaseLabel, componentWavesLabel, resultWaveLabel;
public static JButton play, playTriade, saveImage, saveWave;
public static String info_message = "File saved successfully";
public static String err_message = "File could not be saved";
public static String wave_1 = "Wave 1";
public static String wave_2 = "Wave 2";
public static String wave_3 = "Wave 3";

然后我就可以做我想做的事了。谢谢,祝你好运

不,不幸的是,这并没有改变任何事情。我以前用同样的luckOk将LabelsBundle更改为LabelsBundle,错误消失了。谢谢你。现在让我们转到主要问题,ofc,如果你能提供帮助的话me@Colonder我已经为你的第二个问题添加了解决方案。检查编辑过的答案嗯,好的,但是标签resultWaveLabel在另一个类中…它会工作吗?或者该标签应该在另一个类中定义为静态的?或者在该类中使resultWaveLabel为静态的,并使用StaticClass.resultWaveLabel。或者将该对象传递给MenuBar类。这是基本的。只需谷歌搜索如何从另一个类访问对象。或者在您的帖子中发布其他类代码。