Posted on 2006-02-17 08:56
fortune 閱讀(238)
評論(0) 編輯 收藏 所屬分類:
java技術
在學了這么久的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>