事件处理

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

public class MyJFrame implements ActionListener {
    JFrame f;
    JPanel p;
    JButton b;
    public MyJFrame() {
        f = new JFrame();
        p = new JPanel();
        b = new JButton("变色");
        b.addActionListener(this);
        f.setSize(600,400);
        f.add(p);
        p.add(b);
        f.setVisible(true);    
    }
      
    public static void main(String[] args) {
        new MyJFrame();
        }

    @Override
    public void actionPerformed(ActionEvent e) {
        p.setBackground(Color.blue);
        
    }
}
原文地址:https://www.cnblogs.com/lvzhiqi/p/10850531.html