我正在创建一个应用程序,有两个类 - 两个框架。用户打开应用程序并查看包含3 Jbuttons
的框架,这意味着不同的语言。点击一个特定按钮时,应该打开下一帧,所有标签都选择所选语言。问题是,无论我做什么,它只用一种语言打开。我试过很多解决方案,但它永远不会奏效我会很感激一些代码,这会有所帮助。
头等舱,带有起始帧:
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"));"
不起作用。
答案 0 :(得分:0)
在将buttonBackToChooseLanguage
设置为新值之前,您已在MainFrame
构造函数中设置resourceBundle
的标签 - 而构造函数正在运行时始终设置ResourceBundle.getBundle("labels")
到MainFrame
。
所以MainFrame(ResourceBundle bundle) {
resourceBundle = bundle;
// set up the frame ...
// set labels using resourceBundle
}
看起来像是:
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();
}
});
你的actionListeners看起来像:
for