java事件的小总结

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

JAVA事件的总结

黄玉生、11、14

在学了这么久的JAVA事件总觉的有点搞不清,我想是没有总结的原因吧。下面我将我的几个小例子来更清的了解一下JAVA事件。JAVA事件无非就是键盘事件,鼠标事件,按钮等事件。专业点可以分为语义事件(按钮等到事件)和低层事件(键盘事件,鼠标事件);下面我简要的总结一下: 1、鼠标事件:点鼠标按钮事它会调用三个监听器方法:mousePressed,mouseReleased,mouseClicked.

鼠标事件提供了mousePressed,mouseClicked,mouseDragged,mouseEntered,mouseExited, mouseUp,mouseDown,mouseDrag等事件。下面介绍一个鼠标一例子:

import java.awt.*;

import java.applet.Applet;

public class CountClick extends Applet

{int CurrentMarks=0;

int a,b;

public boolean mouseDown(Event evt,int x,int y)//鼠标按下时做的事

{ CurrentMarks++;//计录按下次数

repaint();//刷新面版

a=x;//得到鼠标的横坐标

b=y;//得到鼠标的竖坐标

return true;

}

public void paint(Graphics g)

{ g.drawString(" "+CurrentMarks,10,10);//打印按下次数

g.drawString(" "+a,10,30);//打印鼠标的横坐标

g.drawString(" "+b,10,20);//打印鼠标的坚坐标

}

}

//<applet code="CountClick.class" width="200" height="100"></applet>

2、键盘事件:如果我们希望使用键盘获得输入信息,就必须处理键盘事件。我们可以用在Conponent的keyDown来实现。如下例子:

import java.applet.Applet;import java.awt.*;

{ char Presskey;

public boolean keyDown(Event evt, int key)

{ Presskey=(char)key;//记录你按下的键

repaint(); return true;

}

public void paint(Graphics g)

{ g.drawString(Presskey,10,10); }//打印你按下的键值

}

3、铵钮等事件:这方面的内容比较多,通过一个例子做一个简单的介绍。

//*******2004年,11月8日**************//

//*******小豆子对事件进一步了解******//

//*******注意Repaint()方法的使用******//

//**通过本程序对JAVA的数据隐藏有了一近一步的了解***///

//*******继续努力。gogogogogo******//

import java.awt.*;

import java.applet.Applet;

import java.awt.event.*;

public class Awtactiontest2 extends Applet implements ItemListener ,ActionListener

//实现ItemListener ,ActionListener接口

{

int num = 5;

Choice ch=new Choice ();

Button one=new Button("one");

Button two=new Button("two");

Button three=new Button("three");

Image aa[];

Image a;

public void init()

{

aa = new Image[num];

for(int i = 0; i < num; i++)//把图片的路径加到数组中存储

{

aa[i] = getImage(getDocumentBase(),"A"+(i+1)+".JPG");

}

num=4;//给一个初值

this.setBackground(Color.white);

ch. addItem("A1.JPG" );

ch. addItem ("A2.JPG" );

ch. addItem ("A3.JPG" );

ch. addItem ("A4.JPG" );

add (ch);

a = getImage(getDocumentBase(),"A1.JPG");//对a一个初值;

add (one);

add (two);

add (three);

ch.addItemListener(this);//注意把ItemListener接口implements进来

one.addActionListener(this);//注意把ActionListener接口implements进来

two.addActionListener(this);

three.addActionListener(this);

}

public void itemStateChanged (ItemEvent e)

{

a = getImage(getDocumentBase(),ch.getSelectedItem ());

repaint();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==one)

{

num=1;

repaint();//对程序刷新

}

if(e.getSource()==two)

{

num=2;

repaint();

}

if(e.getSource()==three)

{

num=3;

repaint();

}

}

public void paint(Graphics g)

{

//g.drawImage(aa[i],0,0,this);

int w=a.getWidth(this);

int h=a.getHeight(this);

// g.drawLine(100,1,200,500);

try{

g.drawImage(a,20,300,10+w,20+h,this);//要抓异常,如果图片找不到呢

g.drawImage(aa[num],50,50,200,200,this);

}

catch(Exception e)

{

System.out.println(e);

}

}

public boolean handleEvent(Event ewt)//关窗体,我原以为这个可以把死循环给关了,其它不然.一样的关不了程序

{

if(ewt.id==Event.WINDOW_DESTROY)

System.exit(0);

else

return super.handleEvent(ewt);

return true;

}

}

//<Applet code="Awtactiontest2.class" width=400 height=500></applet>

注意:由于我还是开始学习写文档,还没有什么水平请大家谅解。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航