在JTextFields中从法语切换到阿拉伯语

时间:2012-08-12 22:28:50

标签: java swing jtextfield

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

2 个答案:

答案 0 :(得分:2)

感谢您的答案,但我找到了问题的解决方案,以下是我如何解决问题:

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);
}          

答案 1 :(得分: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);
    }
}