我正在编写一个Java应用程序而且我只是坚持了一点。我想用菜单项语言制作菜单,这显然会通过点击所需语言来改变应用程序的语言。为此,我编写了两个.properties文件:一个名为LabelsBundle(englisch)的默认文件和一个名为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);
}
}
}
我不知道接下来该做什么。我的队友告诉我关于
的事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
编辑:我想强调的是,主要问题是如何更改上述内容的标签 - 而不是去除提到的错误。这已经解决了。
答案 0 :(得分:1)
<强>异常强>
此输出是预期的,因为项目中没有名为“LabelsBundle”的资源文件或
如果资源文件存在,则会发生同样的情况,但所需的资源不存在。
此外,将此资源文件(称为 LabelsBundle.properties )和您想要的任何资源放在与ResourceBundle.getBundle("newpackage.LabelsBundle", currentLocale);
类相同的目录中。表示java文件和属性文件应位于同一目录中。
如果这个java文件和属性文件在一个包中,你必须提供加载它的完全限定路径
例如,如果属性文件位于名为newpackage的包中,那么您应该加载资源,如
labels
设置标签值
请参阅此link。详情请参阅
在将ResourceBundle分配给变量(您的情况为 resultWaveLabel.setText(labels.getString("blabla"));
)后,只需输入
@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
}
更准确
.productImage
{
width:150px;
height:150px;
vertical-align:central;
padding:10px;
border:10px;
border-radius:50px;
-moz-border-radius:50px;
-webkit-border-radius:50px;
display:block;
Text-indent: -9999px;
}
答案 1 :(得分:0)
作为我自己的问题的回答,在Madhan的帮助下,我的班级现在看起来像这样(声音是其他班级的名字!):
package GuitarPro;
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, program;
private JMenuItem UKLang, DELang, FRLang, ESPLang, POLLang, RUSLang, about, exit;
private ResourceBundle labels;
private Locale currentLocale;
private String aboutApplication;
public MenuBar()
{
menuBar = new JMenuBar();
language = new JMenu("Language");
program = new JMenu("Program");
UKLang = new JMenuItem("English");
DELang = new JMenuItem("German");
FRLang = new JMenuItem("French");
ESPLang = new JMenuItem("Spanish");
POLLang = new JMenuItem("Polish");
RUSLang = new JMenuItem("Russian");
exit = new JMenuItem("Exit");
about = new JMenuItem("About");
UKLang.addActionListener(this);
DELang.addActionListener(this);
POLLang.addActionListener(this);
FRLang.addActionListener(this);
ESPLang.addActionListener(this);
RUSLang.addActionListener(this);
about.addActionListener(this);
exit.addActionListener(this);
language.add(UKLang);
language.add(DELang);
language.add(FRLang);
language.add(POLLang);
language.add(ESPLang);
language.add(RUSLang);
program.add(about);
program.add(exit);
menuBar.add(Box.createVerticalGlue());
menuBar.add(program);
menuBar.add(language);
}
public JMenuBar getMenu()
{
return menuBar;
}
public void setLanguage()
{
Sounds.amplitudeLabel.setText(labels.getString("amplitude_label"));
Sounds.frequencyLabel.setText(labels.getString("frequency_label"));
Sounds.phaseLabel.setText(labels.getString("phase_label"));
Sounds.componentWavesLabel.setText(labels.getString("component_waves_label"));
Sounds.resultWaveLabel.setText(labels.getString("result_wave_label"));
Sounds.play.setText(labels.getString("play_button"));
Sounds.playTriade.setText(labels.getString("play_triade_button"));
Sounds.saveImage.setText(labels.getString("save_image_button"));
Sounds.saveWave.setText(labels.getString("save_sample_button"));
Sounds.info_message = labels.getString("save_success_message");
Sounds.err_message = labels.getString("save_error_message");
Sounds.wave_1 = labels.getString("wave_1");
Sounds.wave_2 = labels.getString("wave_2");
Sounds.wave_3 = labels.getString("wave_3");
aboutApplication = labels.getString("about");
}
@Override
public void actionPerformed(ActionEvent e)
{
Object input = e.getSource();
if(input == UKLang)
{
currentLocale = new Locale("en");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_en", currentLocale);
setLanguage();
}
else if(input == DELang)
{
currentLocale = new Locale("de");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_de", currentLocale);
setLanguage();
}
else if(input == FRLang)
{
currentLocale = new Locale("fr");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_fr", currentLocale);
setLanguage();
}
else if(input == POLLang)
{
currentLocale = new Locale("pl");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_pl", currentLocale);
setLanguage();
}
else if(input == ESPLang)
{
currentLocale = new Locale("esp");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_esp", currentLocale);
setLanguage();
}
else if(input == RUSLang)
{
currentLocale = new Locale("rus");
labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_rus", currentLocale);
setLanguage();
}
else if(input == about)
JOptionPane.showMessageDialog(null, aboutApplication);
else if(input == exit)
dispose();
}
}
我的一个捆绑资源文件如下所示:
# 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";
然后我可以做到我想要的。谢谢,祝你好运!