下面的代码可以实现程序初始化时将焦点定位在任意的一个组件上, 并且在用户按回车键时转移焦点:import java.awt.*;
import java.awt.event.*;public class TestTextField extends Frame { TextField tf1 = new TextField("tf1",8);
TextField tf2 = new TextField("tf2",8); TestTextField(){ this.setTitle( "abc" );
this.setSize( 400, 300 ); this.setLayout( new FlowLayout() ); this.add( tf1 );
this.add( tf2 ); this.show(); tf2.requestFocus(); // 给两个textField加载事件侦听器
tf2.addKeyListener( new KeyAdapter(){
public void keyReleased( KeyEvent e ){
// 这里写相应的处理语句 // 让 tf1 获得焦点
if ( e.getKeyCode() == KeyEvent.VK_ENTER ){
tf1.requestFocus();
}
}
}); tf1.addKeyListener( new KeyAdapter(){
public void keyReleased( KeyEvent e ){
// 这里写相应的处理语句 // 让 tf2 获得焦点
if ( e.getKeyCode() == KeyEvent.VK_ENTER ){
tf2.requestFocus();
}
}
});
} public static void main(String[] args) {
TestTextField app = new TestTextField();
app.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit(0);
}
});
}
}-----------------------------------------
希望对你能有所帮助。
import java.awt.event.*;public class TestTextField extends Frame { TextField tf1 = new TextField("tf1",8);
TextField tf2 = new TextField("tf2",8); TestTextField(){ this.setTitle( "abc" );
this.setSize( 400, 300 ); this.setLayout( new FlowLayout() ); this.add( tf1 );
this.add( tf2 ); this.show(); tf2.requestFocus(); // 给两个textField加载事件侦听器
tf2.addKeyListener( new KeyAdapter(){
public void keyReleased( KeyEvent e ){
// 这里写相应的处理语句 // 让 tf1 获得焦点
if ( e.getKeyCode() == KeyEvent.VK_ENTER ){
tf1.requestFocus();
}
}
}); tf1.addKeyListener( new KeyAdapter(){
public void keyReleased( KeyEvent e ){
// 这里写相应的处理语句 // 让 tf2 获得焦点
if ( e.getKeyCode() == KeyEvent.VK_ENTER ){
tf2.requestFocus();
}
}
});
} public static void main(String[] args) {
TestTextField app = new TestTextField();
app.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit(0);
}
});
}
}-----------------------------------------
希望对你能有所帮助。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货