中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>程序设计>Java>列表

JAVA Component类学习(二)

来源:china1985.cublog.cn 作者: 时间:2007-12-08 点击:

  import java.awt.*;

  import java.awt.event.*;

  public class Test39 extends java.applet.Applet implements ActionListener{

  Button button;

  Label label;

  public void init(){

  Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

  button = new Button("横向走动");

  button.addActionListener(this);

  button.setBackground(Color.red);

  label = new Label("我可以被碰掉",label.CENTER);

  label.setBackground(Color.yellow);

  add(button);add(label);

  }

  

  

  

  public void actionPerformed(ActionEvent e){

  Rectangle rect = button.getBounds();

  if(rect.intersects(label.getBounds())){

  label.setVisible(false);

  }

  if(label.isVisible()){

  button.setLocation(rect.x+3,rect.y);

  }

  else{

  button.setLocation(rect.x+3,rect.y+3);

  button.setLabel("纵向走动");

  }

  }

  }

  =============================================

  1.Rectangle rect = button.getBounds();

  button.getBounds(),是用来返回按钮button的引用,方法返回button的坐标,width height 的值

  这条语句的意思就是把按钮button的数值属性赋值给Rectangle的引用rect,为的是方便判断Labe的相交。

  2.if(rect.intersects(label.getBounds()))

  这条语句的意思就是如果对象引用rect 和 label 相交

  3.label.setVisible(false)

  把标签设置为不可见的

  4.if(label.isVisible)...

  如果标签是可见的话,每当点击按钮一次那么按钮的位置就向x轴方向移动三个像素点

  5.else...

  如果它们已经相交了 标签不可见了。那么现在每点击按钮一次按钮就向x y 轴方向各移动三个像素点

  6.public Rectangle interscection(Rectangle rect)

  得到当前矩形与rect相交部分所构成的矩形,如果当前矩形和rect不相交,就返回null

  7.Rectangle(int x,int y,int width,int heigth)

  创建一个左上角坐标是(x,y),宽是width 高是heigth的矩形

  8.public boolean contains(int x,int y)

  判断点(x,y)是否在当前的矩形内

  9.public boolean contains(int x,int y,int width,int heigth)

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册