Java的事件监听

package 上机;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//完成一个按钮的事件处理程序,实现功能自拟,
//例如:改变窗口的背景颜色,改变按钮的位置等等
import javax.swing.*;
public class bouttona implements ActionListener {
    JFrame f;
    JPanel p;
    JButton b;
    public bouttona() {
        f = new JFrame();
        p = new JPanel();
        b = new JButton("变色");
        b.addActionListener(this);
        f.setSize(400,400);
        f.add(p);
        p.add(b);
        f.setVisible(true);    
    }
      
    public static void main(String[] args) {
        new bouttona();
        }
@Override
public void actionPerformed(ActionEvent e) { p.setBackground(Color.pink); // TODO Auto-generated method stub } }

猜你喜欢

转载自www.cnblogs.com/huangjiaxin/p/10846072.html