问题
I have a form which contains JTextFields, some are specific for French and others for Arabic. i want to switch from a language to another without pressing alt+shift key. any help on the solution will be appreciated. thanks,
回答1:
Thanks aymeric for your answer, but i found a solution for the problem, here is how i resolve the problem:
public void Arabe(JTextField txt) {
txt.getInputContext().selectInputMethod(new Locale("ar", "SA"));
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
public void Français(JTextField txt) {
txt.getInputContext().selectInputMethod(new Locale("fr","FR"));
txt.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
}
private void txt1_FocusGained(java.awt.event.FocusEvent evt) {
Arabe(my_textfields1);
}
private void txt2_FocusGained(java.awt.event.FocusEvent evt) {
Français(mytextfields2);
}
回答2:
The way I understand the question is that you want some specific textfields to be in Arabic (from right to left + with arabic characters) and some other in French.
If your main concern is to avoid the user to press ALT+SHIT, just make your program do it for him :)
This is only a example to get you started (if you didn't find any solution yet):
public class Test {
/**
* This method will change the keyboard layout so that if the user has 2 languages
* installed on his computer, it will switch between the 2
* (tested with french and english)
*/
private static void changeLang() {
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_ALT);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
JTextField arabicTextField = new JTextField();
JTextField frenchTextField = new JTextField();
f.add(frenchTextField, BorderLayout.NORTH);
f.add(arabicTextField, BorderLayout.SOUTH);
frenchTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
changeLang();
}
});
arabicTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
changeLang();
}
});
arabicTextField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
来源:https://stackoverflow.com/questions/11926360/switching-from-french-to-arabic-in-jtextfields