DragonPC_???没有将革命进行到底,想法是对的,试试下面的程序(首先你的系统要支持中文才行):
import javax.swing.*;
import java.awt.im.*;
import java.awt.event.*;
import java.util.*;
public class InputTest implements FocusListener{
JTextField jtf1 = new JTextField();
JTextField jtf2 = new JTextField();
public InputTest(){
JDialog d = new JDialog(new JFrame(), "Test");
d.setBounds(100,100,200,200);
jtf1.setBounds(20,20,80,20);
jtf2.setBounds(20,60,80,20);
d.getContentPane().setLayout(null);
d.getContentPane().add(jtf1);
d.getContentPane().add(jtf2);
d.show();
Locale[] ls = Locale.getAvailableLocales();
jtf1.addFocusListener(this);
jtf2.addFocusListener(this);
}
public void focusGained(FocusEvent e){
boolean bRet = false;
Object obj = e.getSource();
if (obj == jtf1){
InputContext ic = jtf2.getInputContext();
bRet = ic.selectInputMethod(Locale.US);
}else
if (obj == jtf2){
InputContext ic = jtf1.getInputContext();
bRet = ic.selectInputMethod(Locale.SIMPLIFIED_CHINESE);
}
System.out.println("Set " + bRet);
}
public void focusLost(FocusEvent e){
}
public static void main(String[] args){
new InputTest();
}
}