方法一:
//使用动作监听实现焦点转移
package za;
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Text extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
TextField userText, passText;
Button loginButton;
public void init()
{
userText = new TextField(10);
passText = new TextField(10);
loginButton = new Button(" login ");
add(userText);
add(passText);
add(loginButton);
passText.requestFocus(); //开始passText获得焦点
userText.addActionListener(this);//一定不要忘了注册监听
passText.addActionListener(this);
loginButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==userText) //如果userText文本框状态改变(按"Enter"),则触发动作监听,
passText.requestFocus(); //passText获得焦点,即焦点从userText转移到passText
else if(e.getSource()==passText) //如果passText状态改变,则loginButton获得焦点
loginButton.requestFocus();
else if(e.getSource()==loginButton) //如果按钮(loginButton)状态改变(按钮被点击)
userText.requestFocus(); //userText获得焦点
}
}