java 如何使用 VK_UP 或 VK_DOWN 移动到上一个或下一个 Textfield?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11380406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use VK_UP or VK_DOWN to move to the previous or next Textfield?
提问by radik
I want to use the VK_UP or VK_DOWN to move the focus, so it can go to the previous or next textfield.
我想使用 VK_UP 或 VK_DOWN 来移动焦点,因此它可以转到上一个或下一个文本字段。
How can i do that ?
我怎样才能做到这一点 ?
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();
}
}
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.
所以我尝试添加 'System.out.println(c)'
并且给出的结果是空的(空并不意味着像 "" 或 null 这样的空字符串),它更像是 UP 键不起作用。
Thank you so much.
太感谢了。
回答by MvG
Solution ideas
解决思路
Suggesting swing maps
建议挥杆图
Depends on the widget library you use. If you are using Swing, then get the InputMapfrom 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.
取决于您使用的小部件库。如果您使用的是 Swing,则从文本字段中获取InputMap,并为其添加合适的绑定。最初我希望您可以复制 Tab 和 Shift-Tab 的绑定,但正如我在实验中发现的那样,这些并不是InputMap
各个组件的一部分。因此,您必须定义新键,并使用 将ActionMap
它们映射到新操作。
Problem with pasted code
粘贴代码的问题
The code you quoted won't work because you should use getKeyCodeinstead 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_UNDEFINEDinstead.
您引用的代码将不起作用,因为您应该使用getKeyCode而不是getKeyChar。前者对应于那些VK_
常量,而后者将导致字符为“正常”(即打印)键,并且仅在KEY_TYPED
事件期间。对于非打印键,KEY_TYPED
将永远不会生成该事件,而在所有其他事件期间,键字符将改为CHAR_UNDEFINED。
Examples
例子
These examples were added in a later edit.
这些示例是在以后的编辑中添加的。
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.
我对以下代码进行双重许可:您可以根据 CC-Wiki 的条款或 GPL 版本 3 或更高版本的条款使用它。
Example 1: Swing input and action maps
示例 1:Swing 输入和动作映射
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);
}
}
Example 2: AWT KeyListener
示例 2:AWT 密钥侦听器
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);
}
}
回答by Hellephant
I'm no expert, but I don't think Java listens like that when the user is focused on a textfield. Perhaps something like DocumentListenerwould suit your needs?
我不是专家,但我认为当用户专注于文本字段时,Java 不会像那样倾听。也许像DocumentListener这样的东西会满足您的需求?