2012-08-12 66 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); 
    } 
}