两个类内的国际化;资源包
Internationalization within two classes ; resource bundle
我正在创建一个应用程序,其中有两个 classes - 两个框架。用户打开应用程序并看到带有 3 Jbuttons
的框架,这意味着不同的语言。单击一个特定按钮时,它应该打开下一帧,其中包含所选语言的所有标签。问题是,无论我做什么,它都只能以一种语言打开。我尝试了很多解决方案,但从未奏效。我会很感激一些代码,有什么帮助。
第一个 class,起始帧:
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
public class StartFrame extends JFrame {
public StartFrame() {
setSize(480,360);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panelFlags = new JPanel();
this.add(panelFlags);
panelFlags.setBackground(Color.white);
buttonFlagPl= new JButton(new ImageIcon(((new ImageIcon("x.png")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagEn = new JButton(new ImageIcon(((new ImageIcon("y.jpg")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagDe = new JButton(new ImageIcon(((new ImageIcon("z.jpg")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagPl.setPreferredSize(new Dimension(100, 75));
buttonFlagEn.setPreferredSize(new Dimension(100, 75));
buttonFlagDe.setPreferredSize(new Dimension(100, 75));
panelFlags.add(buttonFlagPl);
panelFlags.add(buttonFlagEn);
panelFlags.add(buttonFlagDe);
buttonFlagPl.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFramePl= new MainFrame();
mainFramePl.setVisible(true);
mainFramePl.setLocationRelativeTo(null);
mainFramePl.resourceBundle = ResourceBundle.getBundle("labels_pl", new Locale ("pl"));
dispose();
}
});
buttonFlagEn.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameEn = new MainFrame();
mainFrameEn.setVisible(true);
mainFrameEn.setLocationRelativeTo(null);
mainFrameEn.resourceBundle = ResourceBundle.getBundle("labels_en",new Locale ("en"));
dispose();
}
});
buttonFlagDe.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameDe = new MainFrame();
mainFrameDe.setVisible(true);
mainFrameDe.setLocationRelativeTo(null);
mainFrameDe.resourceBundle = ResourceBundle.getBundle("labels_de",new Locale ("de"));
dispose();
}
});
}
JPanel panelFlags;
JButton buttonFlagPl;
JButton buttonFlagEn;
JButton buttonFlagDe;
public static void main(String[] args) {
StartFrame startFrame = new StartFrame();
startFrame.setVisible(true);
startFrame.setLocationRelativeTo(null);
}
}
这是主框架部分:
public class MainFrame extends JFrame {
public MainFrame(){
super();
setSize(1000,640);
setDefaultCloseOperation(EXIT_ON_CLOSE);
mainPanel = new JPanel(new FlowLayout(500,200,5));
mainPanelColor = new Color (200,200,230);
mainPanel.setBackground(mainPanelColor);
buttonBackToChooseLanguage = new JButton();
buttonBackToChooseLanguage.setText((resourceBundle).getString("button.back"));
(...)
ResourceBundle resourceBundle = ResourceBundle.getBundle("labels");
public static void main(String[] args) {
MainFrame window = new MainFrame();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
我故意漏掉了其余的代码。问题是,无论我在 "ResourceBundle resourceBundle = ResourceBundle.getBundle("labels");"
中输入什么,它总是为所有按钮提供一种相同的语言,所以我假设 "mainFramePl.resourceBundle = ResourceBundle.getBundle("labels_pl", new Locale ("pl"));"
行不起作用。
在将 resourceBundle
设置为新值之前,您在 MainFrame
构造函数中设置了 buttonBackToChooseLanguage
的标签——而构造函数是 运行它总是设置为 ResourceBundle.getBundle("labels")
.
所以 MainFrame
看起来像:
MainFrame(ResourceBundle bundle) {
resourceBundle = bundle;
// set up the frame ...
// set labels using resourceBundle
}
你的 actionListeners 看起来像:
buttonFlagDe.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameDe = new MainFrame(ResourceBundle.getBundle("labels_de",new Locale ("de")));
mainFrameDe.setVisible(true);
mainFrameDe.setLocationRelativeTo(null);
dispose();
}
});
我正在创建一个应用程序,其中有两个 classes - 两个框架。用户打开应用程序并看到带有 3 Jbuttons
的框架,这意味着不同的语言。单击一个特定按钮时,它应该打开下一帧,其中包含所选语言的所有标签。问题是,无论我做什么,它都只能以一种语言打开。我尝试了很多解决方案,但从未奏效。我会很感激一些代码,有什么帮助。
第一个 class,起始帧:
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
public class StartFrame extends JFrame {
public StartFrame() {
setSize(480,360);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panelFlags = new JPanel();
this.add(panelFlags);
panelFlags.setBackground(Color.white);
buttonFlagPl= new JButton(new ImageIcon(((new ImageIcon("x.png")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagEn = new JButton(new ImageIcon(((new ImageIcon("y.jpg")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagDe = new JButton(new ImageIcon(((new ImageIcon("z.jpg")).getImage()).getScaledInstance(100, 75, java.awt.Image.SCALE_SMOOTH)));
buttonFlagPl.setPreferredSize(new Dimension(100, 75));
buttonFlagEn.setPreferredSize(new Dimension(100, 75));
buttonFlagDe.setPreferredSize(new Dimension(100, 75));
panelFlags.add(buttonFlagPl);
panelFlags.add(buttonFlagEn);
panelFlags.add(buttonFlagDe);
buttonFlagPl.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFramePl= new MainFrame();
mainFramePl.setVisible(true);
mainFramePl.setLocationRelativeTo(null);
mainFramePl.resourceBundle = ResourceBundle.getBundle("labels_pl", new Locale ("pl"));
dispose();
}
});
buttonFlagEn.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameEn = new MainFrame();
mainFrameEn.setVisible(true);
mainFrameEn.setLocationRelativeTo(null);
mainFrameEn.resourceBundle = ResourceBundle.getBundle("labels_en",new Locale ("en"));
dispose();
}
});
buttonFlagDe.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameDe = new MainFrame();
mainFrameDe.setVisible(true);
mainFrameDe.setLocationRelativeTo(null);
mainFrameDe.resourceBundle = ResourceBundle.getBundle("labels_de",new Locale ("de"));
dispose();
}
});
}
JPanel panelFlags;
JButton buttonFlagPl;
JButton buttonFlagEn;
JButton buttonFlagDe;
public static void main(String[] args) {
StartFrame startFrame = new StartFrame();
startFrame.setVisible(true);
startFrame.setLocationRelativeTo(null);
}
}
这是主框架部分:
public class MainFrame extends JFrame {
public MainFrame(){
super();
setSize(1000,640);
setDefaultCloseOperation(EXIT_ON_CLOSE);
mainPanel = new JPanel(new FlowLayout(500,200,5));
mainPanelColor = new Color (200,200,230);
mainPanel.setBackground(mainPanelColor);
buttonBackToChooseLanguage = new JButton();
buttonBackToChooseLanguage.setText((resourceBundle).getString("button.back"));
(...)
ResourceBundle resourceBundle = ResourceBundle.getBundle("labels");
public static void main(String[] args) {
MainFrame window = new MainFrame();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
我故意漏掉了其余的代码。问题是,无论我在 "ResourceBundle resourceBundle = ResourceBundle.getBundle("labels");"
中输入什么,它总是为所有按钮提供一种相同的语言,所以我假设 "mainFramePl.resourceBundle = ResourceBundle.getBundle("labels_pl", new Locale ("pl"));"
行不起作用。
在将 resourceBundle
设置为新值之前,您在 MainFrame
构造函数中设置了 buttonBackToChooseLanguage
的标签——而构造函数是 运行它总是设置为 ResourceBundle.getBundle("labels")
.
所以 MainFrame
看起来像:
MainFrame(ResourceBundle bundle) {
resourceBundle = bundle;
// set up the frame ...
// set labels using resourceBundle
}
你的 actionListeners 看起来像:
buttonFlagDe.addActionListener (new ActionListener(){
public void actionPerformed(ActionEvent e) {
MainFrame mainFrameDe = new MainFrame(ResourceBundle.getBundle("labels_de",new Locale ("de")));
mainFrameDe.setVisible(true);
mainFrameDe.setLocationRelativeTo(null);
dispose();
}
});