Java-ActionEvent事件

来源:互联网 发布:齐鲁软件大赛 编辑:程序博客网 时间:2024/05/16 06:49

需要设计一个ActionListener 的接口

package Test;import java.awt.*;import java.awt.event.*;class ColorAction implements ActionListener{private ActionExample frame; //用主类对象作为对象。翻遍两类之间联系private Color bgColor;public ColorAction(ActionExample btn,Color c){//带两个参数构造方法frame = btn;bgColor = c;}public void actionPerformed(ActionEvent e){frame.setBackground(bgColor);}}public class ActionExample extends Frame {ActionExample() {       ///构造函数// TODO Auto-generated constructor stubPanel panel = new Panel();Button redButton = new Button("红色");panel.add(redButton);add(panel, "South");//注册时间侦听器,同一个类的三个不同对象redButton.addActionListener(new ColorAction(this, Color.red));}public static void main(String argc[]){ActionExample myframe = new ActionExample();myframe.setBounds(500, 400, 300, 200);myframe.setVisible(true);myframe.addWindowListener(new WindowAdapter() {  //窗口体关闭public void windowClosing(WindowEvent e){System.exit(0);}});}}


原创粉丝点击