事件委托监听(AWT)

来源:互联网 发布:java编程实例1200例 编辑:程序博客网 时间:2024/06/14 08:29
package Event;/** 事件委托模型:  事件源    事件   监听者  ----设计模式:监听者设计模式* 实现步骤:* 1.让事件源注册事件监听器(添加监听者)  ---btnOk.addXXXListener(this)* 2.让监听者实现监听器接口   ----class MyFrame implements XXXListener* 3.把接口中的方法实现了      ----把接口中的所有抽象方法都实现了*/import java.awt.Button;import java.awt.Color;import java.awt.Event;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Event1 extends Frame implements ActionListener {private TextField tfdName;private TextField tfdPwd;private Button btnOk;private Button btnExit;public Event1(){setTitle("事件委托模型1");setLocation(400,300);setSize(250,3002);setLayout(new FlowLayout());Label lb1 = new Label("Name:");add(lb1);tfdName = new TextField(10);add(tfdName);Label lb2 =new Label("Pwd:");add(lb2);tfdPwd = new TextField(10);add(tfdPwd);btnOk = new Button("Ok");add(btnOk);btnExit = new Button("Exit");add(btnExit);//设置字体Font font = new Font("a",Font.BOLD,18);lb1.setFont(font);lb2.setFont(font);tfdName.setFont(font);tfdPwd.setFont(font);//设置颜色setBackground(Color.pink);//setBackground(Color(168,250,236));btnOk.setForeground(Color.red);btnExit.setForeground(Color.blue);//事件监听实现btnOk.addActionListener(this);btnExit.addActionListener(this);setVisible(true);}     public void actionPerformed(ActionEvent e){     if(e.getSource()==btnOk){     String str =tfdName.getText();     tfdPwd.setText(str);     tfdName.setText("");           }     if(e.getSource()==btnExit){     System.exit(0);     }     }     public static void main(String[] args) {new Event1();}}
package Event;import java.awt.Button;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.Rectangle;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Event2 extends Frame {private TextField tfdName= null;private TextField tfdPwd =null;private Button btnMove;private Button btnExit;public TextField getTfdPwd() {return tfdPwd;}public void setTfdPwd(TextField tfdPwd) {this.tfdPwd = tfdPwd;}public Button getBtnExit() {return btnExit;}public void setBtnExit(Button btnExit) {this.btnExit = btnExit;}public TextField getTfdName() {return tfdName;}public void setTfdName(TextField tfdName) {this.tfdName = tfdName;}public Button getBtnMove() {return btnMove;}public void setBtnMove(Button btnMove) {this.btnMove = btnMove;}public Event2(){super("事件委托模型2");setBackground(Color.pink);setBounds(new Rectangle(new Dimension(240,250)));setLayout(new FlowLayout());add(new Label("Name:"));tfdName = new TextField("NoName",15);add(tfdName);add(new Label("Pwd:"));tfdPwd = new TextField(15);add(tfdPwd);btnMove =new Button("Move");add(btnMove);btnExit =new Button("Exit");add(btnExit);ButtonClickAct abc =new ButtonClickAct(this); //监听者btnMove.addActionListener(abc);btnExit.addActionListener(abc);setVisible(true);}public static void main(String[] args) {new Event2();}}class ButtonClickAct implements ActionListener{private Event2 p=null;public ButtonClickAct(Event2 p){ //通过拷贝构造进行引用赋值对两个类进行数据的交流this.p=p;}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==p.getBtnMove()){p.getTfdPwd().setText(p.getTfdName().getText());p.getTfdName().setText("");}//通过命令字判断事件源----getActionCommand(),这是ActionEvent事件中特有的功能。如果我们没有更改命令字,则默认是按钮上的文字if(e.getActionCommand().equalsIgnoreCase("exit")){  //IgnoreCase()方法可以不用考虑字母的大小System.exit(0);} }}
package Event;import java.awt.Button;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.Rectangle;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JColorChooser;public class Event3 extends Frame {private TextField tfdName=null;private TextField tfdPwd=null;private Button btnMove, btnExit;public Button getBtnMove() {return btnMove;}public void setBtnMove(Button btnMove) {this.btnMove = btnMove;}public TextField getTfdName() {return tfdName;}public void setTfdName(TextField tfdName) {this.tfdName = tfdName;}public TextField getTfdPwd() {return tfdPwd;}public void setTfdPwd(TextField tfdPwd) {this.tfdPwd = tfdPwd;}public Button getBtnExit() {return btnExit;}public void setBtnExit(Button btnExit) {this.btnExit = btnExit;}public Event3() {super("事件委托模型3");setBackground(Color.pink);setBounds( new Rectangle( new Dimension(240, 250)));setLayout(new FlowLayout());add( new Label("Name:"));tfdName = new TextField("NoName",15);add(tfdName );add( new Label("Pwd:"));tfdPwd = new TextField(15);add(tfdPwd );btnMove = new Button("Move");btnExit = new Button("Exit");add(btnMove);add(btnExit);final ButtonClickAct bca = new ButtonClickAct(); //监听者//new内部类的对象时要声明为finalbtnMove.addActionListener( bca );btnExit.addActionListener(bca);//监听器可以采用直接new一个接口的方式来做---匿名内部类,匿名对象---是Java中的一种写实现类的简写方式tfdPwd.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}private Color color =null;@Overridepublic void mouseExited(MouseEvent e) {((TextField)e.getSource()).setBackground(Color.white);Event3.this.setBackground(color);}@Overridepublic void mouseEntered(MouseEvent e) {color = Event3.this.getBackground();((TextField)e.getSource()).setBackground(Color.YELLOW);Event3.this.setBackground(Color.GREEN);}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}});Button btncolor =new Button("Color...");btncolor.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Color c= JColorChooser.showDialog(null, "请选择背景色",Color.white);Event3.this.setBackground(c);btnMove.removeActionListener(bca);//屏蔽该按钮的某部分功能//btnMove.setEnabled(false);//屏蔽该按钮所有功能}});add(btncolor);setVisible(true); }public static void main(String[] args) {new Event3();}class ButtonClickAct implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==btnMove){tfdPwd.setText( tfdName.getText() );tfdName.setText("");}if(e.getActionCommand().equals("Exit")){System.exit(0);}}    }}

1 0
原创粉丝点击