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、铵钮等事件:这方面的内容比较多,通过一个例子做一个简单的介绍。
//
///
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(getdocument.ase(),"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(getdocument.ase(),"A1.JPG");//对a一个初值;
add (one);
add (two);