动作事件监听器——实现ActionListener接口

package swing;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/*
 * 动作事件监听器
 *
 */
//实现ActionListener接口
public class SimpleEvent2 extends JFrame implements ActionListener{  
    private JButton a=new JButton("按钮");  
    public SimpleEvent2(){  
        setTitle("测试动作监听");  
        setVisible(true);  
        setSize(300,300);  
        Container b=getContentPane();  
        b.add(a);  
        a.addActionListener(this);  
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
    }  
    //重写actionPerformed()
    public void actionPerformed(ActionEvent arg0){  
        a.setText("我被单击了");  
    }  
    public static void main(String[] args){  
        new SimpleEvent2();  
    }  


运行结果:

点击之后:



猜你喜欢

转载自blog.csdn.net/qq_41978199/article/details/80642012