public class JTextFieldTest extends JFrame {
public JTextFieldTest() {
// TODO Auto-generated constructor stub
Container c = getContentPane();
setLayout(new FlowLayout());
final JTextField jt = new JTextField("aaa", 20);
final JButton jb = new JButton("清除");
jt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jt.setText("触发事件");
}
});
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jt.setText("");
jt.requestFocus();
}
});
c.add(jt);
c.add(jb);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JTextFieldTest();
}
}