在文本框中输入overnow并且按回车键,文本框自动消失- import java.awt.*;
- import java.awt.event.*;
- class KeyDemo
- {
- Frame f;
- TextField tf;
- KeyDemo()
- {
- init();
- }
- private void init()
- {
- f = new Frame("测试");
- f.setBounds(300,200,500,400);
- f.setLayout(new FlowLayout());
- tf = new TextField(50);
- f.add(tf);
- testEvent();
- f.setVisible(true);
- }
- private void testEvent()
- {
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- tf.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- if(tf.getText().equals("overnow"))
- tf.setVisible(false);
- tf.setText("");
- }
- });
- f.addMouseListener(new MouseAdapter(){
- public void mouseCliked(MouseEvent e)
- {
- tf.setVisible(true);
- }
- });
- }
- public static void main(String[] args)
- {
- new KeyDemo();
- }
- }
复制代码 |