Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1326886
  • 博文数量: 131
  • 博客积分: 9950
  • 博客等级: 中将
  • 技术积分: 1492
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-05 18:32
文章分类

全部博文(131)

文章存档

2008年(3)

2007年(37)

2006年(91)

我的朋友

分类: Java

2007-07-02 03:07:34

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)
判断当前矩形是否包含参数所指定的矩形
10.public Rectangle union(Rectangle rect)
得到同时包含当前矩形和rect的最小矩形

阅读(2719) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~