覆盖 VK_Tab 焦点操作

发布于 2025-01-05 20:18:52 字数 373 浏览 1 评论 0原文

再会!

我正在向 jTextField 添加 keyevent 侦听器,以便如果用户按下 Tab 键,插入符号位置将转到 jtextField 内文本的末尾,这是我的代码:

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
    if(evt.getKeyCode()==KeyEvent.VK_TAB){
        evt.consume();
        jTextField1.setCaretPosition(jTextField1.getText().length());
    }
}

但它不起作用。

我怎样才能做到这一点?

Good Day!

I am tying to add keyevent listener to jTextField so that if the user pressed the tab key, the caret position will go to the end of the text inside the jtextField, here is my code:

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
    if(evt.getKeyCode()==KeyEvent.VK_TAB){
        evt.consume();
        jTextField1.setCaretPosition(jTextField1.getText().length());
    }
}

But it doesn't work.

How can i make this happen?

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

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

发布评论

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

评论(1

你是暖光i 2025-01-12 20:18:52

一种方法是:

  • 首先,不要使用 KeyListener,因为这是一个 Swing 应用程序,并且如果可以避免的话,不应在 Swing 应用程序中使用 KeyListener。
  • 接下来通过 jTextField1.setFocusTraversalKeysEnabled(false); 将 JTextField 的焦点遍历键启用属性设置为 false,
  • 然后使用 键绑定,(同样)不是 KeyListener,用于更改该组件的 Tab 键的行为。

例如:

import java.awt.event.*;
import javax.swing.*;

public class OverrideTab {
   private static void createAndShowGui() {
      JPanel mainPanel = new JPanel();
      final JTextField jTextField1 = new JTextField("This is the text", 20);

      mainPanel.add(new JButton("Here just to get focus"));
      mainPanel.add(jTextField1);

      // just to move the caret to position 0 so we can see the key
      // bindings code in action          
      jTextField1.addFocusListener(new FocusAdapter() {
         @Override
         public void focusGained(FocusEvent e) {
            jTextField1.setCaretPosition(0);
         }
      });

      // turn tab key as focus traversal off for the component
      jTextField1.setFocusTraversalKeysEnabled(false);

      // set the key bindings
      int condition = JComponent.WHEN_FOCUSED;
      InputMap inputMap = jTextField1.getInputMap(condition);
      ActionMap actionMap = jTextField1.getActionMap();
      String tab = "tab";
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), tab);
      actionMap.put(tab, new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            jTextField1.setCaretPosition(jTextField1.getText().length());
            System.out.println("here");
         }
      });



      JFrame frame = new JFrame("OverrideTab");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

当然,此代码将阻止您通过 Shift-Tab 移出 JTextField,因此,如果此行为是必要且重要的,您可以使用 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.emptySet()) 而不是完全禁用焦点遍历。

另外,您想要的行为违背了大多数窗口操作系统的标准,因此您需要有一个非常好的理由来实现这一点,因为您可能会让用户感到困惑。

One way is to:

  • First of all, don't use a KeyListener since this is a Swing application, and you shouldn't use KeyListeners in Swing apps if it can be avoided.
  • Next set the focus traversal keys enabled property of your JTextField to false via jTextField1.setFocusTraversalKeysEnabled(false);
  • Then use key bindings, (again) not a KeyListener, to change the behavior of the tab key for that component.

For example:

import java.awt.event.*;
import javax.swing.*;

public class OverrideTab {
   private static void createAndShowGui() {
      JPanel mainPanel = new JPanel();
      final JTextField jTextField1 = new JTextField("This is the text", 20);

      mainPanel.add(new JButton("Here just to get focus"));
      mainPanel.add(jTextField1);

      // just to move the caret to position 0 so we can see the key
      // bindings code in action          
      jTextField1.addFocusListener(new FocusAdapter() {
         @Override
         public void focusGained(FocusEvent e) {
            jTextField1.setCaretPosition(0);
         }
      });

      // turn tab key as focus traversal off for the component
      jTextField1.setFocusTraversalKeysEnabled(false);

      // set the key bindings
      int condition = JComponent.WHEN_FOCUSED;
      InputMap inputMap = jTextField1.getInputMap(condition);
      ActionMap actionMap = jTextField1.getActionMap();
      String tab = "tab";
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), tab);
      actionMap.put(tab, new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            jTextField1.setCaretPosition(jTextField1.getText().length());
            System.out.println("here");
         }
      });



      JFrame frame = new JFrame("OverrideTab");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Of course this code will prevent you from being able to shift-tab out of the JTextField, and so if this behavior is necessary and important, you could use setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.<KeyStroke>emptySet()) instead of completely disabling focus traversal.

Also, your desired behavior goes against the standards of most windowing operating systems, so you'll want to have a darn good reason for desiring this because you may confuse your users.

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