事件处理程序

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

代码为:

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

public class handle implements ActionListener{
JFrame j;
JButton k;
JPanel s;
JLabel d;

public handle(){
	j=new JFrame("操作窗口");
	k=new JButton("确定改变窗口颜色吗");
	k.addActionListener(this);
	s=new JPanel(null);
    d=new JLabel();
	k.setBounds(150,80,80,40);
	d.setBounds(100, 200, 80, 440);
	s.add(d);
	s.add(k);
	j.add(s);
	j.setVisible(true);
	j.setSize(400, 300);
}

public static void main(String args[]){
	new handle();
}

public void actionPerformed(ActionEvent arg0) {

	s.setBackground(Color.red);
	d.setText("您已成功改变窗口颜色!");
}
}

运行结果为:

 

posted @ 2019-05-08 21:08  罗梦祺  阅读(148)  评论(0)    收藏  举报