1

我有一个包含 JTextFields 的表单,其中一些特定于法语,而另一些则特定于阿拉伯语。我想在不按 alt+shift 键的情况下从一种语言切换到另一种语言。对解决方案的任何帮助将不胜感激。谢谢,

4

2 回答 2

2

感谢 aymeric 的回答,但我找到了解决问题的方法,这是我解决问题的方法:

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);
}          
于 2012-08-17T20:12:39.123 回答
0

我理解这个问题的方式是,您希望一些特定的文本字段使用阿拉伯语(从右到左 + 带有阿拉伯字符),而其他一些文本字段使用法语。

如果您主要关心的是避免用户按 ALT+SHIT,只需让您的程序为他执行此操作 :)

这只是一个让您入门的示例(如果您还没有找到任何解决方案):

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);
    }
}
于 2012-08-16T18:15:58.217 回答