package com.zym.lesson8; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestTextFieldActionEvent { public static void main(String[] args) { //启动 new MyFrame(); } } class MyFrame extends Frame{ public MyFrame(){ TextField textField=new TextField(); add(textField); MyActonListener3 myActonListener3=new MyActonListener3(); //监听这个文本框的输入名字 //按下 enter回车键,就会触发这个输入框 textField.addActionListener(myActonListener3); //对输入框的字进行 加星显示 textField.setEchoChar('*'); setTitle(" 课堂练习,输入框监听"); setSize(200,200); setVisible(true); pack(); addWindowListener(new WindowAdapter() { //窗体关闭时做的事 @Override public void windowClosing(WindowEvent e) { // super.windowClosed(e); System.exit(0); } } ); } } class MyActonListener3 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //获得一些资源 e.getSource(); TextField textField=(TextField) e.getSource(); System.out.println(textField.getText()); } }
标签:awt,java,lesson8,TestTextFieldActionEvent,输入框,import,textField,public From: https://www.cnblogs.com/zym97816/p/17737410.html