我正在创建一个应用程序,有两个类-两个框架。用户打开应用程序并查看带有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"));"
不要工作。