FORTUNE

          THE WAY TO THE MASTER...
          posts - 49, comments - 18, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java事件(轉(zhuǎn))

          Posted on 2006-02-17 08:56 fortune 閱讀(245) 評(píng)論(0)  編輯  收藏 所屬分類: java技術(shù)
          在學(xué)了這么久的JAVA事件總覺的有點(diǎn)搞不清,我想是沒有總結(jié)的原因吧。下面我將我的幾個(gè)小例子來更清的了解一下JAVA事件。JAVA事件無非就是鍵盤事件,鼠標(biāo)事件,按鈕等事件。專業(yè)點(diǎn)可以分為語義事件(按鈕等到事件)和低層事件(鍵盤事件,鼠標(biāo)事件);下面我簡(jiǎn)要的總結(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è)簡(jiǎn)單的介紹。
          //*******2004年,11月8日**************//
          //*******小豆子對(duì)事件進(jìn)一步了解******//
          //*******注意Repaint()方法的使用******//
          //**通過本程序?qū)AVA的數(shù)據(jù)隱藏有了一近一步的了解***///
          //*******繼續(xù)努力。gogogogogo******//
          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ù)組中存儲(chǔ)
          {
          aa[i] = getImage(getDocumentBase(),"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(getDocumentBase(),"A1.JPG");//對(duì)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(getDocumentBase(),ch.getSelectedItem ());
          repaint();
          }
          public void actionPerformed(ActionEvent e)
          {
          if(e.getSource()==one)
          {
          num=1;
          repaint();//對(duì)程序刷新
          }
          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>

          主站蜘蛛池模板: 无为县| 新龙县| 永城市| 汾西县| 富蕴县| 惠来县| 延吉市| 雅江县| 维西| 茌平县| 扎囊县| 右玉县| 监利县| 青州市| 东明县| 东源县| 渑池县| 汝城县| 莱西市| 上蔡县| 东宁县| 香河县| 麦盖提县| 财经| 安丘市| 元谋县| 广德县| 宣化县| 桂平市| 泗水县| 南郑县| 鄂尔多斯市| 如东县| 龙山县| 北川| 新昌县| 湛江市| 华阴市| 和政县| 海兴县| 巴彦县|