代码改变世界

事件处理程序

2019-05-11 23:07  luliya  阅读(147)  评论(0编辑  收藏  举报

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

 

package LL;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class shijian implements ActionListener{
JFrame f;
JPanel p;
JButton b;
JLabel l;
public shijian() {
f=new JFrame();
p=new JPanel(null);
b=new JButton("确定");
l=new JLabel();
b.addActionListener(this);
f.add(p);
p.add(b);
p.add(l);
f.setVisible(true);
f.setSize(400,400);
l.setBounds(50,120,50,50);
b.setBounds(150,150,80,80);
}
public static void main(String[] args) {
new shijian();
}
public void actionPerformed(ActionEvent e) {
p.setBackground(Color.cyan);
l.setText("#include哟");
b.setBounds(250,50,80,80);
}
}