【发布时间】:2017-01-08 09:08:14
【问题描述】:
我正在尝试使用 Swing 的 Window Builder 将字符串外部化为英语,但它根本不起作用。为此,我将进入设计选项卡并选择“世界图标”。然后我选择所有字符串和英语作为新语言环境并翻译内容。然后在创建的类中,我使用 getLocale 和 setIdioma 方法来完成所有工作,但由于某种原因它似乎不起作用......
main frame 我的代码是:
package presentacion;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Prueba {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Prueba window = new Prueba();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Prueba() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblPrueba = new JLabel(Messages.getString("Prueba.lblPrueba.text")); //$NON-NLS-1$
GridBagConstraints gbc_lblPrueba = new GridBagConstraints();
gbc_lblPrueba.insets = new Insets(0, 0, 5, 0);
gbc_lblPrueba.gridx = 1;
gbc_lblPrueba.gridy = 2;
panel.add(lblPrueba, gbc_lblPrueba);
JButton btnJeje = new JButton(Messages.getString("Prueba.btnJeje.text")); //$NON-NLS-1$
btnJeje.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Messages.setIdioma("inglés");
}
});
GridBagConstraints gbc_btnJeje = new GridBagConstraints();
gbc_btnJeje.gridx = 1;
gbc_btnJeje.gridy = 4;
panel.add(btnJeje, gbc_btnJeje);
}
}
Messages.java 的代码:
package presentacion;
import java.beans.Beans;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private Messages() {
}
private static final String BUNDLE_NAME = "presentacion.messages"; //$NON-NLS-1$
private static ResourceBundle RESOURCE_BUNDLE = loadBundle();
private static ResourceBundle loadBundle() {
return ResourceBundle.getBundle(BUNDLE_NAME);
}
public static String getString(String key) {
try {
ResourceBundle bundle = Beans.isDesignTime() ? loadBundle() : RESOURCE_BUNDLE;
return bundle.getString(key);
} catch (MissingResourceException e) {
return "!" + key + "!";
}
}
private static Locale getLocale(String appIdioma){
Locale locale = new Locale("es");
if (appIdioma.equals("inglés"))
locale = new Locale("en");
return locale;
}
public static void setIdioma(String idioma){
RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, getLocale(idioma));
}
}
然后是文件 messages.properties 。 messages_en.properties 也有相同的内容,但字符串是英文的,所以不值得发布 IMO。
Prueba.btnJeje.text=Inglés
Prueba.lblPrueba.text=Prueba
我做错了什么?
【问题讨论】:
-
精确定义“它似乎不起作用”。你在做什么?你期望会发生什么?相反会发生什么?项目中的messages.properties 文件在哪里?
-
更改或“翻译”标签和按钮等文本,因为它是字符串外部化,如标题所述。 messages.properties 与其他文件在同一个包(presentacion)中
-
你回答了我 4 个问题中的最后一个。现在回答另外 3 个人。
-
1.按下按钮时试图翻译它。 2. 需要翻译成英文的文本。 3. 什么都没有。 4. 与其他包在同一个包中,presentacion。
-
当您按下按钮时,您所做的就是拨打
Messages.setIdioma("inglés");。您不会更改按钮和标签的文本。所以他们的文字不会改变。
标签: java string swing internationalization