import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame {
JFrame f;
JButton b;
JPanel p;
public MyFrame(){
f=new JFrame();
f.setTitle("事件处理");
f.setSize(300,300);
p=new JPanel();
p.setLayout(null);
f.add(p);
b=new JButton();
b.setText("点我!!");
p.add(b);
b.setBounds(100, 50, 100, 50);
b.setBackground(Color.green);
b.addActionListener(new bAction());
f.setVisible(true);
}
class bAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
b.setText("我被点击了");
b.setBackground(Color.gray);
}
}
public static void main(String[] args) {
new MyFrame();
}
}


原文:https://www.cnblogs.com/LYY1084702511/p/10850453.html