java applet 中的事件响应

来源:互联网 发布:台服魔兽网络代理 编辑:程序博客网 时间:2024/04/30 13:37

Java.applet.Applet继承了java.awt.Container,作为容器,其中可以添加控件

添加控件的方法有如下几种

Component add(Component comp)           Appends the specified component to the end of this container.Component add(Component comp, int index)           Adds the specified component to this container at the given position.void add(Component comp, Object constraints)           Adds the specified component to the end of this container.void add(Component comp, Object constraints, int index)           Adds the specified component to this container with the specified constraints at the specified index.Component add(String name, Component comp)           Adds the specified component to this container.

如下例,添加组件Label和TextField,并且规定在第二个TextFiled中输入的字符以’*’显示

import java.awt.*;public class cam1 extends java.applet.Applet{TextField name=new TextField(10);TextField pwd=new TextField(10);Label la1=new Label();Label la2=new Label();    public void init()    {    la1.setText("name:");    la2.setText("pwd:");    pwd.setEchoChar('*');    add(la1);    add(name);    add(la2);    add(pwd);    }}

网页打开如图所示


在applet程序中还可以处理事件响应

形式如:事件源a.addXXXListener(事件监听者b)

意思即为控件a的XXX事件被b监听,b在监听到a的XXX事件后可进行响应的操作

例如TextFiled的addActionListener

public voidaddActionListener(ActionListener l)

Adds the specified action listener toreceive action events from this text field. If l is null, no exception isthrown and no action is performed.

接口ActionListener的成员函数为

public void actionPerformed(ActionEvent e) :Invoked whenan action occurs.

下面写一简单的例子,TextFiled1被监听,输入回车时响应,变成字符串mylovestart;pwd1被监听,输入会车时,pwd2中显示pwd1中输入的字符

import java.awt.*;import java.awt.event.*;public class cam1 extends java.applet.Applet{TextField name=new TextField(10);TextField pwd1=new TextField(10);TextField pwd2=new TextField(10);Label la1=new Label();Label la2=new Label();Label la3=new Label();class pwdHandle implements ActionListener{public void actionPerformed(ActionEvent e){pwd2.setText(pwd1.getText());}}class nameHandle implements ActionListener{public void actionPerformed(ActionEvent e){name.setText("mylovestart");}}    public void init()    {    la1.setText("name:");    add(la1);    add(name);    name.addActionListener(new nameHandle());        la2.setText("pwd1");    add(la2);    pwd1.setEchoChar('*');    add(pwd1);    pwd1.addActionListener(new pwdHandle());        la3.setText("Show password");    add(la3);    add(pwd2);    }}

网页打开如下


再看下例,监听的是键盘打印字符的事件,将tf1中打印的字符显示在tf2中

import java.awt.*;import java.awt.event.*;public class cam1 extends java.applet.Applet{TextField tf1=new TextField(10);TextField tf2=new TextField(10);    Label la1=new Label();Label la2=new Label();/*class key implements KeyListener{public void keyPressed(KeyEvent e){//Invoked when a key has been pressed.}public void keyReleased(KeyEvent e) {//Invoked when a key has been released.}        public void keyTyped(KeyEvent e)         {        //Invoked when a key has been typed.         }    }*/class key extends KeyAdapter{public void keyTyped(KeyEvent e){tf2.setText(tf1.getText());}}public void init()    {    la1.setText("tf1:");    add(la1);    add(tf1);        la2.setText("tf2:");    add(la2);    add(tf2);    tf1.addKeyListener(new key());    }}

网页打开如图所示

再看下例,监听button,按一下button,tf中打印一个’*’

import java.awt.*;import java.awt.event.*;public class cam1 extends java.applet.Applet implements ActionListener{TextField tf=new TextField(10);Button bt=new Button();public void init(){add(bt);add(tf);bt.addActionListener(this);}public void actionPerformed(ActionEvent e){tf.setText(tf.getText()+'*');}}

网页打开如下


原创粉丝点击