如何使用VK_UP或VK_DOWN移至上一个或下一个文本字段? [英] How to use VK_UP or VK_DOWN to move to the previous or next Textfield?

查看:164
本文介绍了如何使用VK_UP或VK_DOWN移至上一个或下一个文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用VK_UP或VK_DOWN来移动焦点,因此它可以转到上一个或下一个文本字段.

I want to use the VK_UP or VK_DOWN to move the focus, so it can go to the previous or next textfield.

我该怎么做?

我尝试使用它,但是没有用.

I tried using this, but it didnt work.

 private void passwordTFKeyTyped(java.awt.event.KeyEvent evt) {      
 char c = evt.getKeyChar();

    if (c == KeyEvent.VK_UP) {
        usernameTF.grabFocus();
    }
 }

所以我尝试添加'System.out.println(c)'
并且给出的结果为空(空并不意味着像"或null这样的空字符串),更像是UP键无法正常工作.

So i tried adding 'System.out.println(c)'
and the result given is empty (empty doesnt mean empty string like "" or null), its more like the UP Key isnt working.

非常感谢您.

推荐答案

解决方案

建议挥杆图

取决于您使用的窗口小部件库.如果您使用的是Swing,请获取 InputMap 在文本字段中添加适当的绑定.最初,我希望您可以复制Tab和Shift-Tab的绑定,但是正如我在实验中发现的那样,这些都不属于各个组件的InputMap的一部分.因此,您必须定义新的键,并使用ActionMap将其映射到新的操作.

Solution ideas

Suggesting swing maps

Depends on the widget library you use. If you are using Swing, then get the InputMap from the text field, and add suitable bindings to it. Originally I had hoped that you could copy the bindings for Tab and Shift-Tab, but as I found out in my experiments, those aren't part of the InputMap of the individual components. So you'll have to define new keys, and use the ActionMap to map those to new actions.

您引用的代码无效,因为您应该使用KEY_TYPED事件期间产生.对于非打印键,将永远不会生成KEY_TYPED事件,在所有其他事件中,键字符将为

The code you quoted won't work because you should use getKeyCode instead of getKeyChar. The former corresponds to those VK_ constants, whereas the latter will result in the character for a "normal" (i.e. printing) key, and only during the KEY_TYPED event. For non-printing keys, the KEY_TYPED event will never be generated, and during all other events, the key character will be CHAR_UNDEFINED instead.

这些示例已在以后的编辑中添加.

我双重许可了以下代码:您可以根据CC-Wiki的条款或GPL版本3或更高版本的条款使用它.

I dual-license the following code: you may use it either according to the terms of CC-Wiki or the terms of the GPL version 3 or later.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SO11380406a {
    static final Object focusNextKey = new Object();
    static final Object focusPrevKey = new Object();
    static final Action focusNextAction = new AbstractAction("focusNext") {
            public void actionPerformed(ActionEvent e) {
                ((Component)e.getSource()).transferFocus();
            }
        };
    static final Action focusPrevAction = new AbstractAction("focusPrev") {
            public void actionPerformed(ActionEvent e) {
                ((Component)e.getSource()).transferFocusBackward();
            }
        };
    static final KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
    static final KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
    private static void remap(JComponent c) {
        ActionMap am = new ActionMap();
        am.put(focusNextKey, focusNextAction);
        am.put(focusPrevKey, focusPrevAction);
        am.setParent(c.getActionMap());
        c.setActionMap(am);
        InputMap im = new InputMap();
        im.put(down, focusNextKey);
        im.put(up, focusPrevKey);
        im.setParent(c.getInputMap(JComponent.WHEN_FOCUSED));
        c.setInputMap(JComponent.WHEN_FOCUSED, im);
    }
    public static void main(String[] args) {
        JFrame frm = new JFrame("SO Question 11380406 Demo A");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.getContentPane().setLayout(new GridLayout(2, 1));
        JTextField a = new JTextField(80), b = new JTextField(80);
        frm.getContentPane().add(a);
        frm.getContentPane().add(b);
        frm.pack();
        remap(a);
        remap(b);
        frm.setLocationByPlatform(true);
        frm.setVisible(true);
    }
}

示例2:AWT KeyListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SO11380406b {
    static final KeyListener arrowFocusListener = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getModifiers() == 0) {
                    if (e.getKeyCode() == KeyEvent.VK_DOWN)
                        e.getComponent().transferFocus();
                    if (e.getKeyCode() == KeyEvent.VK_UP)
                        e.getComponent().transferFocusBackward();
                }
            }
        };
    private static void remap(Component c) {
        c.addKeyListener(arrowFocusListener);
    }
    public static void main(String[] args) {
        JFrame frm = new JFrame("SO Question 11380406 Demo B");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.getContentPane().setLayout(new GridLayout(2, 1));
        JTextField a = new JTextField(80), b = new JTextField(80);
        frm.getContentPane().add(a);
        frm.getContentPane().add(b);
        frm.pack();
        remap(a);
        remap(b);
        frm.setLocationByPlatform(true);
        frm.setVisible(true);
    }
}

这篇关于如何使用VK_UP或VK_DOWN移至上一个或下一个文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆