java 使用 Tab 键从 JTextArea 移动焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525855/
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
Moving focus from JTextArea using tab key
提问by
As stated, I want to change the default TAB behaviour within a JTextArea(so that it acts like a JTextFieldor similar component)
如上所述,我想更改 a 中的默认 TAB 行为JTextArea(使其像一个JTextField或类似的组件一样)
Here's the event action
这是事件操作
private void diagInputKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.KEY_PRESSED == java.awt.event.KeyEvent.VK_TAB) {
actionInput.transferFocus();
}
}
And here's the listener
这是听众
diagInput.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
diagInputKeyPressed(evt);
}
});
I've tried evt.KEY_TYPED as well with no joy.
我也试过 evt.KEY_TYPED 也没有任何乐趣。
Any ideas?
有任何想法吗?
quick edit: I've also tried requestFocus()in place of transferFocus()
快速编辑:我也试过requestFocus()代替transferFocus()
采纳答案by VonC
According to this class:
根据这个类:
/**
* Some components treat tabulator (TAB key) in their own way.
* Sometimes the tabulator is supposed to simply transfer the focus
* to the next focusable component.
* <br/>
* Here s how to use this class to override the "component's default"
* behavior:
* <pre>
* JTextArea area = new JTextArea(..);
* <b>TransferFocus.patch( area );</b>
* </pre>
* This should do the trick.
* This time the KeyStrokes are used.
* More elegant solution than TabTransfersFocus().
*
* @author kaimu
* @since 2006-05-14
* @version 1.0
*/
public class TransferFocus {
/**
* Patch the behaviour of a component.
* TAB transfers focus to the next focusable component,
* SHIFT+TAB transfers focus to the previous focusable component.
*
* @param c The component to be patched.
*/
public static void patch(Component c) {
Set<KeyStroke>
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);
}
}
Note that patch()can be even shorter, according to Joshua Goldbergin the comments, since the goal is to get back default behaviors overridden by JTextArea:
请注意patch(),根据Joshua Goldberg在评论中的说法,它甚至可以更短,因为目标是恢复被以下内容覆盖的默认行为JTextArea:
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERS??AL_KEYS, null);
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERS??AL_KEYS, null);
This is used in question "How can I modify the behavior of the tab key in a JTextArea?"
这用于问题“如何修改 a 中 tab 键的行为JTextArea?”
The previous implementationinvolved indeed a Listener, and the a transferFocus():
在以前的实现确实是一个参与Listener,和一个transferFocus():
/**
* Override the behaviour so that TAB key transfers the focus
* to the next focusable component.
*/
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_TAB) {
System.out.println(e.getModifiers());
if(e.getModifiers() > 0) a.transferFocusBackward();
else a.transferFocus();
e.consume();
}
}
e.consume();might have been what you missed to make it work in your case.
e.consume();可能是您错过了使其在您的情况下工作的原因。

