我尝试使用Swing中的Window Builder将字符串外部化为英语,但它根本不起作用。为此,我将进入设计选项卡并选择" World Icon"。然后我选择所有字符串和英语作为新语言环境并翻译内容。然后在创建的类中,我使用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
也有Prueba.btnJeje.text=Inglés
Prueba.lblPrueba.text=Prueba
但字符串是英文的,因此不值得发布IMO。
{{1}}
我可能做错了什么?
答案 0 :(得分:0)
好的,所以我在运行时做了一个解决方案,就像我想的那样。为此,我刚刚处理了调用方法的框架,并从同一帧中创建了一个实例:
构造函数已更改为:
public Prueba() {
initialize();
frame.setVisible(true);
}
按钮侦听器已更改为:
btnJeje.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Messages.setIdioma("inglés");
frame.dispose();
new Prueba();
}