java按回车键,光标自动定位到下一个文本框中,
展开全部
import java.awt.FlowLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FocusDemo extends JFrame
{
public FocusDemo()
{
MyTextField txt1 = new MyTextField("test1");
MyTextField txt2 = new MyTextField("test2");
MyTextField txt3 = new MyTextField("test3");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(txt1);
getContentPane().add(txt2);
getContentPane().add(txt3);
}
public static void main(String[] args)
{
FocusDemo f = new FocusDemo();
f.setSize(300, 300);
f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
f.pack();
f.setVisible(true);
}
class MyTextField extends JTextField
{
public MyTextField(String str)
{
super(str);
this.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
e.consume();
KeyboardFocusManager.
getCurrentKeyboardFocusManager().focusNextComponent();
}
}
});
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询