事件处理程序

完成一个按钮的事件处理程序,实现功能自拟,例如:改变窗口的背景颜色,改变按钮的位置等等。

package 事件处理;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class xy implements ActionListener{
JFrame f;
JPanel p;
JButton b;
public xy() {
f=new JFrame("窗口");
p=new JPanel();
b=new JButton("点击一下");
b.addActionListener(this);
p.add(b);
f.add(p);
f.setSize(400,200);
f.setVisible(true);

}
public static void main(String[] args) {
new xy();
}
public void actionPerformed(ActionEvent arg) {
p.setBackground(Color.red);
}

}

运行结果:

posted @ 2019-05-06 17:54  海诺音  阅读(102)  评论(0编辑  收藏  举报