在 JTextfield 中保留键盘布局?

发布于 2024-07-18 07:35:33 字数 118 浏览 12 评论 0原文

简单的例子:2 个JTextFields,一个用于西班牙语单词,另一个用于其翻译。 有没有办法保留每个 JTextField 的键盘布局,以便用户不必来回切换?

TIA。

Simple example: 2 JTextFields, one for a spanish word another one for it's translation.
Is there a way to preserve keyboard layout per JTextField so that the user wouldn't have to switch back and forth?

TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城烟雨 2024-07-25 07:35:33

是的,此演示代码使用每个文本字段中选定区域设置的键盘布局:在

public class InputMethodTest {

  public static void main(String[] args) {
    final InputContext en = InputContext.getInstance();
    en.selectInputMethod(Locale.UK);
    final InputContext es = InputContext.getInstance();
    es.selectInputMethod(new Locale("es", "ES"));
    JTextArea english = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return en;
      }
    };
    JTextArea spanish = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return es;
      }
    };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout());
    frame.getContentPane().add(new JScrollPane(english));
    frame.getContentPane().add(new JScrollPane(spanish));
    frame.setSize(600, 400);
    frame.setVisible(true);
  }
}

安装了 EN 和 ES 键盘布局的 Windows XP Home 上进行测试(通过控制面板 > 区域和语言选项 > 语言 > 详细信息...) 。 有关更多详细信息,请参阅 Java 输入法框架

Yes, this demo code uses the keyboard layout for the selected locales in each text field:

public class InputMethodTest {

  public static void main(String[] args) {
    final InputContext en = InputContext.getInstance();
    en.selectInputMethod(Locale.UK);
    final InputContext es = InputContext.getInstance();
    es.selectInputMethod(new Locale("es", "ES"));
    JTextArea english = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return en;
      }
    };
    JTextArea spanish = new JTextArea() {
      @Override
      public InputContext getInputContext() {
        return es;
      }
    };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout());
    frame.getContentPane().add(new JScrollPane(english));
    frame.getContentPane().add(new JScrollPane(spanish));
    frame.setSize(600, 400);
    frame.setVisible(true);
  }
}

Tested on Windows XP Home with EN and ES keyboard layouts installed (via Control Panel > Regional and Language Options > Languages > Details...). See the Java Input Method Framework for more details.

⒈起吃苦の倖褔 2024-07-25 07:35:33

不,键盘布局由操作系统或桌面环境管理。

No, keyboard layouts are managed by the OS or desktop environment.

沧笙踏歌 2024-07-25 07:35:33

如果您确切地知道相关西班牙语键盘的布局,理论上您可以自己处理 KeyEvents,将它们翻译成适当的字符。 然而,这并不是一件容易的事。 您最终可能会自己将字符插入文本字段。

If you know exactly the layout of the Spanish keyboard in question you could theoretically process KeyEvents yourself, translating them into the appropriate character. However this would not be an easy thing to do. You would probably end up inserting characters into the textfields yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文