日期:2014-05-20 浏览次数:20883 次
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Text { public void startup() { final JFrame frame = new JFrame("Just a demo"); final JButton north = new JButton("North"); final JTextArea text = new JTextArea(15,20); final JScrollPane scroller = new JScrollPane(text); final JButton south = new JButton("South"); frame.add(north, BorderLayout.PAGE_START); frame.add(scroller, BorderLayout.CENTER); frame.add(south, BorderLayout.PAGE_END); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Action next = new AbstractAction("jumptonext"){ @Override public void actionPerformed(ActionEvent e){ KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(text); } }; text.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0), "jumpnext"); text.getActionMap().put("jumpnext",next); frame.addWindowListener(new WindowAdapter(){ @Override public void windowOpened(WindowEvent e){ text.requestFocusInWindow(); } }); } public static void main(final java.lang.String[] args) { EventQueue.invokeLater(new java.lang.Runnable(){ @Override public void run(){ new Text().startup(); } }); } }
------解决方案--------------------
楼上的方法可以借鉴下。
你给JTextArea添加Tab事件
当触发Tab的时候你就在TextArea2中获取焦点。