日期:2014-05-20 浏览次数:20819 次
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Example10_7 { public static void main(String[] args) { MyWindow win = new MyWindow(); } } class MyWindow extends JFrame { JTextField text; Polices police; MyWindow() { setLayout(new FlowLayout()); text = new JTextField(15); add(text); police = new Polices(text); text.addActionListener(police); setBounds(300, 400, 200, 150); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class Polices implements ActionListener { JTextField t; public Polices(JTextField textfield) { t = textfield; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); System.out.println(str); System.out.println(str.length()); t.setText(""); } }