栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何修改JTextArea中Tab键的行为?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何修改JTextArea中Tab键的行为?

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 Keysn2n3n4n5n6n7n8n9");        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 Actionsn2n3n4n5n6n7n8n9");        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 Listenern2n3n4n5n6n7n8n9");        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 Keysn2n3n4n5n6n7n8n9");        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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号