【问题标题】:Cannot change input language in jTextField无法更改 jTextField 中的输入语言
【发布时间】:2014-10-24 15:47:57
【问题描述】:

为什么我不能在 jtextfield 和JOptionPane.showInputDialog() 中切换输入语言? 在我的电脑上可以,但在其他电脑上我只能写系统语言符号。

Ctrl+ShiftCtrl+Alt+Shift 不起作用在应用程序中,但当应用程序没有焦点时它正在工作

Locale.setDefault(Locale.ENGLISH); //tried it
System.setProperty("user.language", "en"); // and it

private void showPasswordWindow() {

    String pass = JOptionPane.showInputDialog(null, "Enter password", "Secure", JOptionPane.WARNING_MESSAGE);
    if (pass == null)
        System.exit(0);
    if (!pass.contains("somepassword"))
        showPasswordWindow();
}

不起作用((密码包含英文符号,我无法输入密码(只有俄罗斯符号有效)

JRE 8; PS:我想输入英文符号,但我只能输入俄文符号...NOT WORKING ONLY DIALOG TEXTFIELDS

【问题讨论】:

    标签: java swing jtextfield


    【解决方案1】:

    这里有两个问题。

    首先是输入符号。这并不是真正的 Java 问题,而是 installing a different language for the keyboard 的问题。

    第二个问题是显示外语符号。您可能需要使用 UTF 字体来显示否则无法正确显示的字符

    【讨论】:

    • 我知道如何在我的电脑上更改语言,当应用程序窗口聚焦时我无法更改
    【解决方案2】:

    你应该改变Locale:

    textField.getInputContext().selectInputMethod(Locale.ENGLISH);
    

    编辑

    也许这不是最好的选择,但您可以尝试为JtextField 覆盖默认的DocumentFilter 并更改编码:

    class EnglishTextField extends JTextField {
    
        public EnglishTextField() {
            ((AbstractDocument)getDocument()).setDocumentFilter(new DocumentFilter(){
                @Override
                public void insertString(DocumentFilter.FilterBypass fb, int offset,
                        String text, AttributeSet attr) throws BadLocationException {
                    try {
                        //TODO change to your current encoding
                        byte[] bytes = text.getBytes("ISO-8859-1"); 
                        fb.insertString(offset, new String(bytes, "UTF-8"), attr);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 有没有办法将其设置为全局?或者我需要对所有文本字段都这样做?以及如何为 JOptionPane.showInputDialog 做到这一点?
    • 是的。使用Locale.setDefault(Locale.ENGLISH);
    • 不工作,同样的问题 - coderanch.com/t/559975/java/java/…
    • 尝试更改编码。
    • 对话框文本域怎么做? showInputDialog();
    【解决方案3】:

    终于……我做到了O_O

    frame.setVisible(true);
    

    解决了问题,但我不明白为什么??..

    附言没有我的 comp 应用程序上的代码运行良好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多