JAVA事件的總結(jié)(轉(zhuǎn))- -

                                      

在阿泠的自省上看到的文章,雖然我現(xiàn)在還沒有看到事件,不過先收錄過來^_^。

JAVA事件無非就是鍵盤事件,鼠標(biāo)事件,按鈕等事件。專業(yè)點(diǎn)可以分為語義事件(按鈕等到事件)和低層事件(鍵盤事件,鼠標(biāo)事件);

下面我簡要的總結(jié)一下:

1、鼠標(biāo)事件:點(diǎn)鼠標(biāo)按鈕事它會(huì)調(diào)用三個(gè)監(jiān)聽器方法:mousePressed,mouseReleased,mouseClicked.


鼠標(biāo)事件提供了mousePressed,mouseClicked,mouseDragged,mouseEntered,mouseExited, mouseUp,mouseDown,mouseDrag等事件。下面介紹一個(gè)鼠標(biāo)一例子:
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)//鼠標(biāo)按下時(shí)做的事
 { CurrentMarks++;//計(jì)錄按下次數(shù)
   repaint();//刷新面版
   a=x;//得到鼠標(biāo)的橫坐標(biāo)
   b=y;//得到鼠標(biāo)的豎坐標(biāo)
   return true;
 }
 public void paint(Graphics g)
 { g.drawString(" "+CurrentMarks,10,10);//打印按下次數(shù)
   g.drawString(" "+a,10,30);//打印鼠標(biāo)的橫坐標(biāo)
   g.drawString(" "+b,10,20);//打印鼠標(biāo)的堅(jiān)坐標(biāo)
 }
}
//<applet code="CountClick.class" width="200" height="100"></applet>      

      
   2、鍵盤事件:如果我們希望使用鍵盤獲得輸入信息,就必須處理鍵盤事件。我們可以用在Conponent的keyDown來實(shí)現(xiàn)。如下例子:
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、銨鈕等事件:這方面的內(nèi)容比較多,通過一個(gè)例子做一個(gè)簡單的介紹。

//*******注意Repaint()方法的使用******//
//**通過本程序?qū)AVA的數(shù)據(jù)隱藏有了一近一步的了解***///

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Awtactiontest2 extends Applet implements ItemListener ,ActionListener
//實(shí)現(xiàn)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++)//把圖片的路徑加到數(shù)組中存儲
        {
            aa[i] = getImage(getdocument.a(chǎn)se(),"A"+(i+1)+".JPG");
        }
        num=4;//給一個(gè)初值
 this.setBackground(Color.white);
 ch. addItem("A1.JPG" );
 ch. addItem ("A2.JPG" );
 ch. addItem ("A3.JPG" );
 ch. addItem ("A4.JPG" );
 add (ch);
 a = getImage(getdocument.a(chǎn)se(),"A1.JPG");//對a一個(gè)初值;
 add (one);
 add (two);
 add (three);
 ch.addItemListener(this);//注意把ItemListener接口implements進(jìn)來
 one.addActionListener(this);//注意把ActionListener接口implements進(jìn)來
 two.addActionListener(this);
 three.addActionListener(this);
 }
public void itemStateChanged (ItemEvent e)
  {
   
    a = getImage(getdocument.a(chǎn)se(),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)//關(guān)窗體,我原以為這個(gè)可以把死循環(huán)給關(guān)了,其它不然.一樣的關(guān)不了程序
 {
         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>