所以我正在编写一个程序,我必须将语言改为另一种语言,我已完成该部分,但我似乎无法让JComboBox更改语言,例如,如果用户单击日语中的语言JComboBox从英语变为Eigo,法语变为Furansugo是否有一段代码可以专门执行此操作并将其与ActionPerformed一起用于语言?
你可能不需要它,但这是我的代码:
/**
*
*/
package gui;
import javax.swing.*;
import gui.GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* @author Michelle
*
*/
public class GUI extends JFrame implements ActionListener {
ResourceBundle res, res1, res2;
JFrame frame;
JButton button, button1, button2, button3, button4;
JLabel label, label1,label2,label3, label4;
JScrollPane output;
JComboBox<String> combo;
String[] array;
public GUI()
{
Locale loc = new Locale("fr"); //create Locale for French in France
res = res.getBundle("programResource.ProgramResource_fr",loc); //Create resource bundle
res1 = res1.getBundle("programResource.ProgramResource_jp",loc);
res2 = res2.getBundle("programResource.ProgramResource_en",loc);
Container c = getContentPane();
JPanel p = new JPanel();
String[] array = {"English", "French", "Japanese"};
combo = new JComboBox<String>(array);
output = new JScrollPane(combo);
combo.addActionListener(this);
JMenuBar mb = new JMenuBar();
mb.add(output);
setJMenuBar(mb);
label = new JLabel();
label1 = new JLabel();
label2= new JLabel();
label3 = new JLabel();
label4 = new JLabel();
ImageIcon apple = new ImageIcon("apple.jpg");
label.setIcon(apple);
button = new JButton("Apple");
button.addActionListener(this);
ImageIcon banana = new ImageIcon("banana.jpg");
label1.setIcon(banana);
button1 = new JButton("banana");
button1.addActionListener(this);
ImageIcon grapes = new ImageIcon("grapes.jpg");
label2.setIcon(grapes);
button2 = new JButton("Grapes");
button2.addActionListener(this);
ImageIcon orange = new ImageIcon("orange.jpg");
label3.setIcon(orange);
button3 = new JButton("Orange");
button3.addActionListener(this);
ImageIcon pear = new ImageIcon("pear.jpg");
label4.setIcon(pear);
button4 = new JButton("Pear");
button4.addActionListener(this);
p.add(label);
p.add(label1);
p.add(label2);
p.add(label3);
p.add(label4);
p.add(button);
p.add(button1);
p.add(button2);
p.add(button3);
p.add(button4);
c.add(p);
setSize(425,300);
setVisible(true);
}
//French
public void actionPerformed(ActionEvent a) {
Button(a);
Button1(a);
Button2(a);
Button3(a);
Button4(a);
}
public void Button(ActionEvent a) {
if (a.getSource() instanceof JComboBox) {
// TODO Auto-generated method stub
JComboBox ref = (JComboBox)a.getSource();
if(ref.getSelectedItem().equals("French"))
{
button.setText(res.getString("computeButton"));
}
else if(ref.getSelectedItem().equals("Japanese"))
{
button.setText(res1.getString("computeButton1_jp"));
}
else if(ref.getSelectedItem().equals("English"))
{
button.setText(res2.getString("computeButton_en"));
}
}
}
public void Button1(ActionEvent a) {
if (a.getSource() instanceof JComboBox) {
// TODO Auto-generated method stub
JComboBox ref = (JComboBox)a.getSource();
if(ref.getSelectedItem().equals("French"))
{
button1.setText(res.getString("computeButton1"));
}
else if(ref.getSelectedItem().equals("Japanese"))
{
button1.setText(res1.getString("computeButton1_jp"));
}
else if(ref.getSelectedItem().equals("English"))
{
button1.setText(res2.getString("computeButton1_en"));
}
}
}
public void Button2(ActionEvent a) {
if (a.getSource() instanceof JComboBox) {
// TODO Auto-generated method stub
JComboBox ref = (JComboBox)a.getSource();
if(ref.getSelectedItem().equals("French"))
{
button2.setText(res.getString("computeButton2"));
}
else if(ref.getSelectedItem().equals("Japanese"))
{
button2.setText(res1.getString("computeButton2_jp"));
}
else if(ref.getSelectedItem().equals("English"))
{
button2.setText(res2.getString("computeButton2_en"));
}
}
}
public void Button3(ActionEvent a) {
if (a.getSource() instanceof JComboBox) {
// TODO Auto-generated method stub
JComboBox ref = (JComboBox)a.getSource();
if(ref.getSelectedItem().equals("French"))
{
button3.setText(res.getString("computeButton3"));
}
else if(ref.getSelectedItem().equals("Japanese"))
{
button3.setText(res1.getString("computeButton3_jp"));
}
else if(ref.getSelectedItem().equals("English"))
{
button3.setText(res2.getString("computeButton3_en"));
}
}
}
public void Button4(ActionEvent a) {
if (a.getSource() instanceof JComboBox) {
// TODO Auto-generated method stub
JComboBox ref = (JComboBox)a.getSource();
if(ref.getSelectedItem().equals("French"))
{
button4.setText(res.getString("computeButton4"));
}
else if(ref.getSelectedItem().equals("Japanese"))
{
button4.setText(res1.getString("computeButton4_jp"));
}
else if(ref.getSelectedItem().equals("English"))
{
button4.setText(res2.getString("computeButton4_en"));
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:0)
我会尝试这个例子正确定义你的听众肯定会有所帮助。 我也不明白为什么你有3个捆绑实例...你应该根据所需的语言有1个..
Button button1 = new Button();
button1.setCaption(ResourceBundle.getBundle("yourfile",your_locale).getLabel("fruit.banana"));
button1.setIcon(CommonIcons.ADD);
button1.addListener(new ClickListener()
{
private static final long serialVersionUID = 7855693595481663167L;
@Override
public void buttonClick(ClickEvent event)
{
handleAddButton();
}
});
我认为你应该对此好。我还建议你创建一个jpgs数组和一个JButton数组...并在循环中初始化它们。这样你就可以管理n个对象,而不是复制凌乱的代码......