Changing/Switching Windows 7 输入语言使用 java
Changing/Switching the Windows 7 input language using java
阅读过类似的旧问题none,其中对我的案例有帮助。
OS: Windows 7 x64
语言输入:
我想将 马拉雅拉姆语(印度)[Google Input tools]
的语言切换到 Java Swing
应用程序中的一个 jTextField
,然后再切换回其余字段为英语(美国)。
更新:
字段 malyalamField
是 javax.swing.JTextField
javax.swing.JTextField malyalamField= new javax.swing.JTextField();
malyalamField.setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
马拉雅拉姆语(印度): 南印度喀拉拉邦使用的地方语言。
我试图通过在 FocusGained
和 FocusGained
事件上从 java 触发 Alt + LShift 组合键来实现
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]);
(见 post 底部)
这有时有效,有时无效。观察到一些不稳定的行为。有时它不会在 FocusLost
事件上变回 English(United States),即使在代码执行后也是如此。
我也试过使用
malyalamField.enableInputMethods(true);
malyalamField.getInputContext().selectInputMethod(new Locale("MY", "IN"));
但这也没有用,selectInputMethod()
返回 NULL
我可能在做 selectInputMethod()
时用错了 Locale
到目前为止,我所拥有的最好的方法是从 java
触发击键。有什么方法可以实现吗?
示例代码:
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;
}
这是您需要的语言环境:
Locale loc = new Locale("ml", "IN");
malayalamFileld.setLocale(loc);
malayalamFileld.getInputContext().selectInputMethod(loc);
但您需要知道,如果启动您的程序的用户是 Administrator/Root!!!
,此 仅 有效
阅读过类似的旧问题none,其中对我的案例有帮助。
OS: Windows 7 x64
语言输入:
我想将 马拉雅拉姆语(印度)[Google Input tools]
的语言切换到 Java Swing
应用程序中的一个 jTextField
,然后再切换回其余字段为英语(美国)。
更新:
字段 malyalamField
是 javax.swing.JTextField
javax.swing.JTextField malyalamField= new javax.swing.JTextField();
malyalamField.setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
马拉雅拉姆语(印度): 南印度喀拉拉邦使用的地方语言。
我试图通过在 FocusGained
和 FocusGained
事件上从 java 触发 Alt + LShift 组合键来实现
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]);
(见 post 底部)
这有时有效,有时无效。观察到一些不稳定的行为。有时它不会在 FocusLost
事件上变回 English(United States),即使在代码执行后也是如此。
我也试过使用
malyalamField.enableInputMethods(true);
malyalamField.getInputContext().selectInputMethod(new Locale("MY", "IN"));
但这也没有用,selectInputMethod()
返回 NULL
我可能在做 selectInputMethod()
时用错了 Locale
到目前为止,我所拥有的最好的方法是从 java
触发击键。有什么方法可以实现吗?
示例代码:
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;
}
这是您需要的语言环境:
Locale loc = new Locale("ml", "IN");
malayalamFileld.setLocale(loc);
malayalamFileld.getInputContext().selectInputMethod(loc);
但您需要知道,如果启动您的程序的用户是 Administrator/Root!!!
,此 仅 有效