2012-08-12 43 views
1

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

+1

不太明白的问题,所以盲目拍摄:myTextFiel d.setLocale(...)?还是关于如何将该操作分配给keyStroke?如果是这样,看看KeyBindings(在swing标签中引用的教程中) – kleopatra 2012-08-13 06:36:50

+0

我想设置一个Jtextfield的语言环境,我使用这个代码,但它不起作用:// private void issmMouseClicked(java.awt。 event.MouseEvent evt){Locale l = new Locale(“ar”); mytextfield.setLocale(l); mytextfield.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } – aoulhent 2012-08-13 20:46:22

回答

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