比如说在登录框中,输完用户密码后直接回车就相当于单击了确定按钮,这个怎么实现?
输入完成一个文本框中的数据回车后下一个文本框获得焦点,这个又怎么实现?
各位老大能不能贴段实现此功能的代码上来,谢谢了!!!
------解决方案--------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TrasverseFocusUsingEnter implements ActionListener
{
JFrame mainFrame;
JPanel mainPanel;
JTextField textField1;
JTextField textField2;
public TrasverseFocusUsingEnter(){
mainFrame = new JFrame ( );
mainPanel = new JPanel ();
textField1 = new JTextField(10);
textField2 = new JTextField(10);
textField1.addActionListener( this );
textField2.addActionListener( this );
mainPanel.add( textField1 );
mainPanel.add( textField2 );
mainFrame.getContentPane().add( mainPanel );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible( true );
}
public void actionPerformed( ActionEvent e ){
FocusManager.getCurrentManager().focusNextComponent( (JTextField)e.getSource() );
}
public static void main(String[] args)
{
new TrasverseFocusUsingEnter();
}
}