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 } }
原文:https://www.cnblogs.com/huangjiaxin/p/10846072.html