<small id='e89Vz'></small><noframes id='e89Vz'>

    1. <legend id='e89Vz'><style id='e89Vz'><dir id='e89Vz'><q id='e89Vz'></q></dir></style></legend>
    2. <i id='e89Vz'><tr id='e89Vz'><dt id='e89Vz'><q id='e89Vz'><span id='e89Vz'><b id='e89Vz'><form id='e89Vz'><ins id='e89Vz'></ins><ul id='e89Vz'></ul><sub id='e89Vz'></sub></form><legend id='e89Vz'></legend><bdo id='e89Vz'><pre id='e89Vz'><center id='e89Vz'></center></pre></bdo></b><th id='e89Vz'></th></span></q></dt></tr></i><div id='e89Vz'><tfoot id='e89Vz'></tfoot><dl id='e89Vz'><fieldset id='e89Vz'></fieldset></dl></div>
        <tfoot id='e89Vz'></tfoot>
          <bdo id='e89Vz'></bdo><ul id='e89Vz'></ul>

        如何修改 JTextArea 中制表键的行为?

        时间:2024-08-24

          <bdo id='h7vpk'></bdo><ul id='h7vpk'></ul>

            • <tfoot id='h7vpk'></tfoot>

              <legend id='h7vpk'><style id='h7vpk'><dir id='h7vpk'><q id='h7vpk'></q></dir></style></legend>

              <small id='h7vpk'></small><noframes id='h7vpk'>

                <tbody id='h7vpk'></tbody>
              • <i id='h7vpk'><tr id='h7vpk'><dt id='h7vpk'><q id='h7vpk'><span id='h7vpk'><b id='h7vpk'><form id='h7vpk'><ins id='h7vpk'></ins><ul id='h7vpk'></ul><sub id='h7vpk'></sub></form><legend id='h7vpk'></legend><bdo id='h7vpk'><pre id='h7vpk'><center id='h7vpk'></center></pre></bdo></b><th id='h7vpk'></th></span></q></dt></tr></i><div id='h7vpk'><tfoot id='h7vpk'></tfoot><dl id='h7vpk'><fieldset id='h7vpk'></fieldset></dl></div>
                1. 本文介绍了如何修改 JTextArea 中制表键的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在Java Swing 中创建一个表单,其中一个字段是JTextArea.当我在所有其他字段上使用 Tab 键时,它会将焦点转移到下一个小部件,但在 JTextArea 中,它会在文本.

                  I'm creating a form in Java Swing, and one of the fields is a JTextArea. When I use the Tab key on all other fields, it gives the focus to the next widget, but in the JTextArea, it inserts a tab character (horizontal space) in the text.

                  如何修改此行为?

                  推荐答案

                  /*
                      This is my understanding of how tabbing works. The focus manager
                      recognizes the following default KeyStrokes for tabbing:
                  
                      forwards:  TAB or Ctrl-TAB
                      backwards: Shift-TAB or Ctrl-Shift-TAB
                  
                      In the case of JTextArea, TAB and Shift-TAB have been removed from
                      the defaults which means the KeyStroke is passed to the text area.
                      The TAB KeyStroke inserts a tab into the Document. Shift-TAB seems
                      to be ignored.
                  
                      This example shows different approaches for tabbing out of a JTextArea
                  
                      Also, a text area is typically added to a scroll pane. So when
                      tabbing forward the vertical scroll bar would get focus by default.
                      Each approach shows how to prevent the scrollbar from getting focus.
                  */
                  import java.awt.*;
                  import java.util.*;
                  import java.awt.event.*;
                  import javax.swing.*;
                  
                  public class TextAreaTab extends JFrame
                  {
                      public TextAreaTab()
                      {
                          Container contentPane = getContentPane();
                          contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
                  
                          contentPane.add( nullTraversalKeys() );
                          contentPane.add( writeYourOwnAction() );
                          contentPane.add( useKeyListener() );
                          contentPane.add( addTraversalKeys() );
                      }
                  
                      //  Reset the text area to use the default tab keys.
                      //  This is probably the best solution.
                  
                      private JComponent nullTraversalKeys()
                      {
                          JTextArea textArea = new JTextArea(3, 30);
                  
                          textArea.setText("Null Traversal Keys
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9");
                          JScrollPane scrollPane = new JScrollPane( textArea );
                  //        scrollPane.getVerticalScrollBar().setFocusable(false);
                  
                          textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
                          textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
                  
                          return scrollPane;
                      }
                  
                      //  Replace the Tab Actions. A little more complicated but this is the
                      //  only solution that will place focus on the component, not the
                      //  vertical scroll bar, when tabbing backwards (unless of course you
                      //  have manually prevented the scroll bar from getting focus).
                  
                      private JComponent writeYourOwnAction()
                      {
                          JTextArea textArea = new JTextArea(3, 30);
                          textArea.setText("Write Your Own Tab Actions
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9");
                          JScrollPane scrollPane = new JScrollPane( textArea );
                  
                          InputMap im = textArea.getInputMap();
                          KeyStroke tab = KeyStroke.getKeyStroke("TAB");
                          textArea.getActionMap().put(im.get(tab), new TabAction(true));
                          KeyStroke shiftTab = KeyStroke.getKeyStroke("shift TAB");
                          im.put(shiftTab, shiftTab);
                          textArea.getActionMap().put(im.get(shiftTab), new TabAction(false));
                  
                          return scrollPane;
                      }
                  
                      //  Use a KeyListener
                  
                      private JComponent useKeyListener()
                      {
                          JTextArea textArea = new JTextArea(3, 30);
                          textArea.setText("Use Key Listener
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9");
                          JScrollPane scrollPane = new JScrollPane( textArea );
                          scrollPane.getVerticalScrollBar().setFocusable(false);
                  
                          textArea.addKeyListener(new KeyAdapter()
                          {
                              public void keyPressed(KeyEvent e)
                              {
                                  if (e.getKeyCode() == KeyEvent.VK_TAB)
                                  {
                                      e.consume();
                                      KeyboardFocusManager.
                                          getCurrentKeyboardFocusManager().focusNextComponent();
                                  }
                  
                                  if (e.getKeyCode() == KeyEvent.VK_TAB
                                  &&  e.isShiftDown())
                                  {
                                      e.consume();
                                      KeyboardFocusManager.
                                          getCurrentKeyboardFocusManager().focusPreviousComponent();
                                  }
                              }
                          });
                  
                          return scrollPane;
                      }
                  
                      //  Add Tab and Shift-Tab KeyStrokes back as focus traversal keys.
                      //  Seems more complicated then just using null, but at least
                      //  it shows how to add a KeyStroke as a focus traversal key.
                  
                      private JComponent addTraversalKeys()
                      {
                          JTextArea textArea = new JTextArea(3, 30);
                          textArea.setText("Add Traversal Keys
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9");
                          JScrollPane scrollPane = new JScrollPane( textArea );
                          scrollPane.getVerticalScrollBar().setFocusable(false);
                  
                          Set set = new HashSet( textArea.getFocusTraversalKeys(
                              KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS ) );
                          set.add( KeyStroke.getKeyStroke( "TAB" ) );
                          textArea.setFocusTraversalKeys(
                              KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set );
                  
                          set = new HashSet( textArea.getFocusTraversalKeys(
                              KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS ) );
                          set.add( KeyStroke.getKeyStroke( "shift TAB" ) );
                          textArea.setFocusTraversalKeys(
                              KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set );
                  
                          return scrollPane;
                      }
                  
                      class TabAction extends AbstractAction
                      {
                          private boolean forward;
                  
                          public TabAction(boolean forward)
                          {
                              this.forward = forward;
                          }
                  
                          public void actionPerformed(ActionEvent e)
                          {
                              if (forward)
                                  tabForward();
                              else
                                  tabBackward();
                          }
                  
                          private void tabForward()
                          {
                              final KeyboardFocusManager manager =
                                  KeyboardFocusManager.getCurrentKeyboardFocusManager();
                              manager.focusNextComponent();
                  
                              SwingUtilities.invokeLater(new Runnable()
                              {
                                  public void run()
                                  {
                                      if (manager.getFocusOwner() instanceof JScrollBar)
                                          manager.focusNextComponent();
                                  }
                              });
                          }
                  
                          private void tabBackward()
                          {
                              final KeyboardFocusManager manager =
                                  KeyboardFocusManager.getCurrentKeyboardFocusManager();
                              manager.focusPreviousComponent();
                  
                              SwingUtilities.invokeLater(new Runnable()
                              {
                                  public void run()
                                  {
                                      if (manager.getFocusOwner() instanceof JScrollBar)
                                          manager.focusPreviousComponent();
                                  }
                              });
                          }
                      }
                  
                      public static void main(String[] args)
                      {
                          TextAreaTab frame = new TextAreaTab();
                          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
                          frame.pack();
                          frame.setLocationRelativeTo(null);
                          frame.setVisible(true);
                      }
                  }
                  

                  这篇关于如何修改 JTextArea 中制表键的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是好的 Java 日期选择器 Swing GUI 小部件? 下一篇:RXTX 的稳定替代品

                  相关文章

                      <legend id='RkWyt'><style id='RkWyt'><dir id='RkWyt'><q id='RkWyt'></q></dir></style></legend>
                    1. <i id='RkWyt'><tr id='RkWyt'><dt id='RkWyt'><q id='RkWyt'><span id='RkWyt'><b id='RkWyt'><form id='RkWyt'><ins id='RkWyt'></ins><ul id='RkWyt'></ul><sub id='RkWyt'></sub></form><legend id='RkWyt'></legend><bdo id='RkWyt'><pre id='RkWyt'><center id='RkWyt'></center></pre></bdo></b><th id='RkWyt'></th></span></q></dt></tr></i><div id='RkWyt'><tfoot id='RkWyt'></tfoot><dl id='RkWyt'><fieldset id='RkWyt'></fieldset></dl></div>

                        <bdo id='RkWyt'></bdo><ul id='RkWyt'></ul>
                    2. <tfoot id='RkWyt'></tfoot>

                      <small id='RkWyt'></small><noframes id='RkWyt'>