2015-04-07 59 views
14

已經閱讀了與此類似的舊問題,但他們都沒有幫助我解決問題。使用java更改/切換Windows 7輸入語言

操作系統:Windows 7 64位系統

語言輸入:

enter image description here

我想語言切換到馬拉雅拉姆語(印度)[Google Input tools]只有一個jTextFieldJava Swing申請並切換回英語(美國)爲其餘領域。

UPDATE:

領域malyalamFieldjavax.swing.JTextField

 javax.swing.JTextField malyalamField= new javax.swing.JTextField(); 
     malyalamField.setFont(new java.awt.Font("Arial Unicode MS", 0, 12)); 

馬拉雅拉姆語(印度):名爲喀拉拉邦南部印度國家使用的區域語言。

我試圖從Java上FocusGainedFocusGained事件

int keyInput[] = { KeyEvent.VK_ALT, KeyEvent.VK_SHIFT}; 
Robot shiftkey=null; 
Robot altkey=null; 
try { 
    shiftkey = new Robot(); 
    altkey=new Robot(); 
} catch (AWTException e) { 
    e.printStackTrace(); 
} 

altkey.keyPress(keyInput[0]); 
shiftkey.keyPress(keyInput[1]); 

altkey.keyRelease(keyInput[0]); 
shiftkey.keyRelease(keyInput[1]); 

燒製Alt + LSHIFT組合鍵來實現這一目標(見職位的底部)

這有時有效,有時它沒有觀察到一點不穩定的行爲。有時,即使代碼執行後,它也不會變回English(United States) on FocusLost事件。
enter image description here

我也使用

  malyalamField.enableInputMethods(true); 
     malyalamField.getInputContext().selectInputMethod(new Locale("MY", "IN")); 

嘗試,但這個也沒有工作,selectInputMethod()返回NULL

我可能有錯誤的Locale

做了selectInputMethod()作爲現在我所擁有的最棒的是從012開始擊鍵。有沒有辦法實現這個?

示例代碼:

import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 

public class LanguageChangeForm extends javax.swing.JFrame { 
    public LanguageChangeForm() { 
     initComponents(); 
    } 
    @SuppressWarnings("unchecked") 
    private void initComponents() { 

     englishField1 = new javax.swing.JTextField(); 
     malayalamFileld = new javax.swing.JTextField(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     englishField2 = new javax.swing.JTextField(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     malayalamFileld.setFont(new java.awt.Font("Arial Unicode MS", 0, 11)); 
     malayalamFileld.addFocusListener(new java.awt.event.FocusAdapter() { 
      public void focusGained(java.awt.event.FocusEvent evt) { 
       malayalamFileldFocusGained(evt); 
      } 
      public void focusLost(java.awt.event.FocusEvent evt) { 
       malayalamFileldFocusLost(evt); 
      } 
     }); 

     jLabel1.setText("English Field"); 
     jLabel2.setText("Malayalam Field"); 
     jLabel3.setText("English Field"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(47, 47, 47) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
          .addComponent(englishField2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE) 
          .addComponent(englishField1)) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(malayalamFileld, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(34, 34, 34)))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(49, 49, 49) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jLabel2)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(englishField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(malayalamFileld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addComponent(jLabel3) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(englishField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(152, Short.MAX_VALUE)) 
     ); 

     pack(); 
    } 

    private void malayalamFileldFocusGained(java.awt.event.FocusEvent evt) { 
     try{ 
     int keyInput[] = { KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,,KeyEvent.VK_2}; 
     Robot robitkey=null; 

     try { 
      robitkey= new Robot(); 

     } catch (AWTException e) { 

      e.printStackTrace(); 
     } 
      robitkey.keyPress(keyInput[0]); 
      robitkey.keyPress(keyInput[1]); 
      robitkey.keyPress(keyInput[2]); 
      robitkey.keyRelease(keyInput[0]); 
      robitkey.keyRelease(keyInput[1]); 
      robitkey.keyRelease(keyInput[2]); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    private void malayalamFileldFocusLost(java.awt.event.FocusEvent evt) { 


     try{ 
     int keyInput[] = { KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,,KeyEvent.VK_1}; 
     Robot robitkey=null; 

     try { 
      robitkey= new Robot(); 

     } catch (AWTException e) { 

      e.printStackTrace(); 
     } 
      robitkey.keyPress(keyInput[0]); 
      robitkey.keyPress(keyInput[1]); 
      robitkey.keyPress(keyInput[2]); 
      robitkey.keyRelease(keyInput[0]); 
      robitkey.keyRelease(keyInput[1]); 
      robitkey.keyRelease(keyInput[2]); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 
    public static void main(String args[]) { 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(LanguageChangeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(LanguageChangeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(LanguageChangeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(LanguageChangeForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 

     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new LanguageChangeForm().setVisible(true); 
      } 
     }); 
    } 
    private javax.swing.JTextField englishField1; 
    private javax.swing.JTextField englishField2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JTextField malayalamFileld; 

} 
+1

什麼類型是'malyalamField'請張貼一個例子,我們可以很容易地重現。 –

+0

@ Jean-FrançoisSavard已添加要求的詳細信息關於malayalamField –

+0

@AndrewThompson,已添加示例代碼的鏈接。 –

回答

3

這是你需要的語言環境:

Locale loc = new Locale("ml", "IN"); 
malayalamFileld.setLocale(loc); 
malayalamFileld.getInputContext().selectInputMethod(loc); 

但是你要知道,這作品只有即使啓動程序的用戶是管理員/根!

+0

換句話說,一個無用的功能:S用uac顯示器打擾用戶不是特別用戶友好的。 – Tschallacka

+0

這看起來像是最可能的解決方案,很快就會進行測試。 UAC不是問題。 On始終可以禁用。 –

+0

提供的鏈接現在不起作用。 – ThilinaMD