展开全部
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Awt extends Frame{
TextField tf = new TextField();
int year,month;
public static void main(String[] args) {
new Awt();
}
public Awt() {
setLayout(null);
setBounds(300, 200, 400, 400);
setTitle("按键事件");
// 设置窗口是否可变大小
setResizable(false);
setVisible(true);
//关闭窗口功能
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf.setBounds(40, 40, 50, 20);
//为输入框增加事件监听,输入数32313133353236313431303231363533e59b9ee7ad9431333332616439字,敲回车响应事件
tf.addActionListener(new TfListener());
add(tf);
}
private class TfListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = tf.getText().trim();
year = Integer.parseInt(str);
System.out.println(year);
tf.setText("");
}
}
}
上面的代码可以给你参考,执行main方法,在输入框中输入数字,敲回车,数字就会传到后台的成员变量"year"中,然后你可以做想要的操作