DemoJava

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTextFieldDemo extends JFrame {
    public JTextFieldDemo() {
        this.setTitle("JTextFieldDemo");
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.UI();
        this.setVisible(true);
    }
    private void UI() {
        this.setLayout(new FlowLayout());
        final JTextField jf = new JTextField("aaa", 20);
        final JButton jb = new JButton("清除");
        this.add(jf);
        this.add(jb);
        jf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                jf.setText("触发事件");
            }
        });
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                jf.setText("");
                jf.requestFocus();
            }
        });
    }

    public static void main(String[] args) {
        new JTextFieldDemo();
    }
}

 

你可能感兴趣的:(Java)