Java Component类的常用方法

来源:互联网 发布:js 工作流引擎 编辑:程序博客网 时间:2024/05/20 03:47

Component类是所有组件的父类。

  一、组件的颜色

  1.public void setBackground(Color c);  //设置组件的背景色

  2.public void setForeground(Color c);  //设置组件的前景色

  3.public Color getBackground(Color c); //获取组件的背景色

  4.public Color getForeGround(Color c); //获取组件的前景色

  Color类是Java.awt包中的类。用Color类的构造方法public Color(int red,int green,int blue)可以创建一个颜色对象,三个颜色值取值都在0-255之间。Color类还有red,blue,green,orange,cyan,yellow,pink等静态常量。

  二、组件的字体

  1.public void setFont(Font f);  //设置组件上的字体

  2.public Font getFont(Font f);  //获取组件上的字体

  3.public Font(String name,int style,int size);  //构造方法

  1) name:字体名称,如果系统中无该字体,则取默认的字体名字。

  2) style:字体的式样,取值是一个整数,其有效取值为:

    Font.BOLD,Font.PLAIN,Font.ITALIC,

    Font.ROMAN_BASELINE,Font_CENTER_BASELINE,Font.HANGING_BASELINE,

    FOnt.TRUETYPE_FONT

  3) size:字体的大小,单位是磅(如5号字体是12磅)。

  4.获取系统中有哪些字体名字可用的方法:

   GraphicsEnvironment ge=GraphicsEnvironment getLocalGrphicsEnvironment();

   String fontName[]=ge.getAvailableFontFamilyNames();

  三、组件的大小与位置

  1.public void setSize(int width,int height);  //设置组件的大小(宽度和高度)

  2.public void setLocation(int x,int y);  //设置组件在容器中的位置,左上角坐标为(0,0)

  3.public Dimension getSize();  //返回组件的大小(组件的宽度和高度)

  4.public Point getLocation(int x,int y);  //返回组件有容器中的位置(左上角坐标)

  5.public void setBounds(int x,int y,int width,int height);  //设置组件在容器中的位置及组件大小

  6.public Rectangle getBounds();  //返回组件在容器中的位置和大小

   Rectangle对象的常用方法:

   1) Rectangle(int x,int y,int width,int height);  //构造方法

   2) public boolean intersects(Rectangle rect);  //判断当前矩形是否和rect相交

   3) public boolean contains(int x,int y);  //判断当前点(x,y)是否在当前矩形内

   4) public boolean contains(int x,int y,int width,int height);//矩形是否包含参数指定的矩形

   5) public boolean contains(Rectangle rect); //同上

   6) public Rectangle intersection(Rectangle rect);  //得到当前矩形与rect相交部分所构成的矩形,如不相交则返回null。

   7) public Rectangle union(Rectangle rect);  //得到同时包含当前矩形和rect的最小矩形

  四、组件的激活与可见性

  1.public void setEnabled(boolean b);  //设置组件是否被激活

  2.public boolean isEnabled();  //判断组件是否为激活状态

  3.public void setCisible(boolean b);  //设置组件是否可见

  4.public boolean isVisible();  //判断组件是否可见

  五、组件上的光标

  1.public void setCursor(Cursor c);  //设置鼠标指向组件时的形状,Cursor类的常量有:

    HAND_CURSOR,       CROSSHAIR_CURSOR,   TEXT_CURSOR,        WAIT_CURSOR,  

      SW_RESIZE_CURSOR,  SE_RESIZE_CURSOR,   NW_RESIZE_CURSOR,   NE_RESIZE_CURSOR,

      N_RESIZECURSOR,    S_RESIZE_CURSOR,    W_RESIZE_CURSOR,    E_RESIZE_CURSOR,

      MOVE_CURSOR,       CUSTOM_CURSOR

  2.Cursor c=new Cursor(Cursor.HAND_CURSOR);  //创建一个手形状的光标对象

  3.Cursor c=Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); //直接获得一个光标对象

  六、paint方法与repaint方法

  public void paint(Graphics g);

  repaint方法首先清除paint方法所画内容,然后再调用paint方法。

0 0
原创粉丝点击