我正在用java创建一个桌面程序,在这个程序中有一个JOptionPane
来选择用户想要使用的语言,但是当用户选择其中一个选项时,我的组件的文本不会真实的更新。
我使用.properties
文件作为语言 例如,语言选项中提供:
en.properties
selectOptions=Select one of the options below:
startButton=START
optionsButton=SETTINGS
jp.properties
selectOptions=以下のオプションから選択してください:
startButton=スタート
optionsButton=設定
在我的LanguageManager
类中,我配置了我的密钥的get,我有updateLanguage
方法,但是当我使用updateLanguage
方法时,它改变了我的语言,但文本没有改变。
public void updateLanguage(String selectedLang, String selectedLocale) {
lang = selectedLang;
locale = selectedLocale;
setLanguage(lang, locale);
}
根据我的研究,执行更新的最常见方法是创建一个方法来更新我的组件的文本,但由于我有几个组件,我必须向我的方法传递几个参数,它类似于下面的示例:
public void updateTexts(JButton button1, JButton button2, JLabel label1, JLabel label2){
button1.setText(languagerManager.getButton1("button1"))
button2.setText(languagerManager.getButton2("button2"))
label1.setText(languagerManager.getButton1("label1"))
label2.setText(languagerManager.getButton1("label2"))
}
但我认为这样做效率不高。
下面是我的类以及我在选择另一种语言时如何尝试实现此更新。
主屏幕类别:
private LanguageManager languageManager;
public MainScreen() {
languageManager = LanguageManager.getInstance();
startButton = new JButton(languageManager.getStartButton());
rankingButton = new JButton(languageManager.getRankingButton());
optionsButton = new JButton(languageManager.getOptionsButton());
exitButton = new JButton(languageManager.getExitButton());
optionsButton.addActionListener(e -> Buttons.optionsButton(e));
}
我打开类的语言设置:
public class Buttons {
static public void optionsButton(ActionEvent e) {
LanguageManager languageManager = new LanguageManager();
String[] languages = {"Português", "English", "Español", "日本"};
JComboBox<String> comboBox = new JComboBox<>(languages);
JPanel panel = new JPanel();
panel.add(comboBox);
int result = JOptionPane.showConfirmDialog(null, panel, "Selecione uma linguagem", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.OK_OPTION){
String selectedLang = "";
String selectedLocale = "";
switch(comboBox.getSelectedIndex()) {
case 0: // Português
selectedLang = "pt";
selectedLocale = "pt_BR";
break;
case 1: // English
selectedLang = "en";
selectedLocale = "en";
break;
case 2: // Español
selectedLang = "es";
selectedLocale = "es";
break;
case 3: // 日本
selectedLang = "jp";
selectedLocale = "jp";
break;
}
languageManager.updateLanguage(selectedLang, selectedLocale);
}
}
}
负责语言的类:
public class LanguageManager {
private static LanguageManager instance;
private ResourceBundle messages;
private String lang, locale;
public LanguageManager() {
setLanguage("en","en");
}
public static LanguageManager getInstance() {
if (instance == null) {
instance = new LanguageManager();
}
return instance;
}
public void updateLanguage(String selectedLang, String selectedLocale) {
lang = selectedLang;
locale = selectedLocale;
setLanguage(lang, locale);
}
public void setLanguage(String messageFile, String languageCode) {
messages = ResourceBundle.getBundle("ApsProject.src.Languages." + messageFile, new Locale(languageCode));
}
public String getTitleAps() {
return messages.getString("titleAps");
}
public String getSelectOptions() {
return messages.getString("selectOptions");
}
public String getNumberOfParticipants() {
return messages.getString("numberOfParticipants");
}
public String getNameOfParticipantes() {
return messages.getString("nameOfParticipantes");
}
public String getStartButton() {
return messages.getString("startButton");
}
public String getMatchButton() {
return messages.getString("matchButton");
}
public String getOptionsButton() {
return messages.getString("optionsButton");
}
public String getSimulationButton() {
return messages.getString("simulationButton");
}
public String getSaveButton() {
return messages.getString("saveButton");
}
public String getRankingButton() {
return messages.getString("rankingButton");
}
public String getBackButton() {
return messages.getString("backButton");
}
public String getExitButton() {
return messages.getString("exitButton");
}
public String getUserOption() {
return messages.getString("userOption");
}
public String getAutoOption() {
return messages.getString("autoOption");
}
public String getPlayer1() {
return messages.getString("player1");
}
public String getPlayer2() {
return messages.getString("player2");
}
public String getPlayer3() {
return messages.getString("player3");
}
public String getPlayer4() {
return messages.getString("player4");
}
public String getPlayer5() {
return messages.getString("player5");
}
public String getTeam1() {
return messages.getString("team1");
}
public String getTeam2() {
return messages.getString("team2");
}
public String getTeam3() {
return messages.getString("team3");
}
public String getTeam4() {
return messages.getString("team4");
}
public String getTeam5() {
return messages.getString("team5");
}
public String getLap1Player1() {
return messages.getString("lap1Player1");
}
public String getLap1Player2() {
return messages.getString("lap1Player2");
}
public String getLap1Player3() {
return messages.getString("lap1Player3");
}
public String getLap1Player4() {
return messages.getString("lap1Player4");
}
public String getLap1Player5() {
return messages.getString("lap1Player5");
}
public String getLap2Player1() {
return messages.getString("lap2Player1");
}
public String getLap2Player2() {
return messages.getString("lap2Player2");
}
public String getLap2Player3() {
return messages.getString("lap2Player3");
}
public String getLap2Player4() {
return messages.getString("lap2Player4");
}
public String getLap2Player5() {
return messages.getString("lap2Player5");
}
public String getTotalTimeP1() {
return messages.getString("totalTimeP1");
}
public String getTotalTimeP2() {
return messages.getString("totalTimeP2");
}
public String getTotalTimeP3() {
return messages.getString("totalTimeP3");
}
public String getTotalTimeP4() {
return messages.getString("totalTimeP4");
}
public String getTotalTimeP5() {
return messages.getString("totalTimeP5");
}
public String getPlayer() {
return messages.getString("Player");
}
}
如果有人知道我能做什么,我会很感激。
真实的更新语言。
1条答案
按热度按时间t3psigkw1#
经过几次尝试,我得到了它.这里是更新的代码:
Buttons
类:如何调用
optionsButton
方法,以及如何定义MainScreen
中的文本:在我的
updateLanguage
方法中,我更新了语言,我的新屏幕将已经使用所选语言创建,我必须做的是更新我的MainScreen
的语言,因为它已经打开并加载了字符串