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()+'*');}}
网页打开如下
- java applet 中的事件响应
- Applet事件响应
- Applet事件响应
- JAVA中的Applet介绍
- Java Script响应HTML表单中的事件
- applet 事件--java学习笔记
- Java Applet编程响应鼠标键盘
- DIRECTSHOW中的响应事件
- java 事件响应
- JAVA事件响应机制
- VC.NET中的事件响应
- vc.net中的事件响应...
- 关于响应事件中的Sender
- NGUI中的事件响应流程
- 编写一个Applet响应鼠标事件,用户可以通过拖动鼠标在Applet中画出蓝色的圆点
- 在Java Applet中的动画编程【转载】
- Java applet 在JSP中的使用
- java.applet.Applet类
- MFC的消息處理函數
- js 页面获取坐标点
- Ubuntu下如何解压zip,tar,tar.gz,tar.bz2文件
- UFT Mobile – The Official HP Mobile Testing Solution
- zoj 1402 Magnificent Meatballs
- java applet 中的事件响应
- 如何做好软件工程质量管理
- 如何去写 Android init.rc
- 需求文档编写建议
- Android自动化测试“Adb connection Error:远程主机强迫关闭了一个现有的连接”错误解决方案
- BOOTCLASSPATH
- 以太网最大帧和最小帧、MTU
- Windows 资源管理器的音乐文件星星评级的ID3v2标签
- 职责链模式