Java中的null布局

来源:互联网 发布:安卓魔音通话变声软件 编辑:程序博客网 时间:2024/05/22 14:03

Java中的null布局,就是没有布局,或称空布局。

null布局和Component类的setBounds方法结合使用,便可以进行这种不受限制的布局。

进行null布局,setBounds方法是关键,其函数如下:

public void setBounds(int x,int y,int width,int height)

  其中x、y表示组件右上角坐标,width、height分别表示组件的宽和高,单位为像素。

案例如下:

package lay;

import java.awt.*;
import java.awt.event.*;

public class layoutnull extends Frame
{
 Panel pnl;
 Button button1,button2,button3,button4;
 layoutnull()
 {
  super("Frame with null Layout");
  setSize(300,250);

  setVisible(true);
          setLayout(null);//创建一个null布局
  pnl=new Panel(); //创建面板pn1
  pnl.setBackground(Color.blue);
  add(pnl); //添加面板
  button1=new Button("按钮1");//创建按钮button1
  button2=new Button("按钮2");
  button3=new Button("按钮3");
  button4=new Button("按钮4");
  add(button1); //添加按钮button1
  add(button2);
  add(button3);
  add(button4);
  pnl.setBounds(20,40,200,190); //面板pn1的左上角坐标为(20,40),宽为200像素,高为190像素
  button1.setBounds(230,60,50,30); //按钮button1的左上角坐标为(230,60),宽为50像素,高为30像素
  button2.setBounds(230,100,50,30);
  button3.setBounds(230,140,50,30);
  button4.setBounds(230,180,50,30);
  addWindowListener(
   new WindowAdapter(){   
    public void windowClosing(WindowEvent e){
     setVisible(false);
     System.exit(0);}});
 }

 public static void main(String args[])
 {
  layoutnull frm=new layoutnull();//创建一个layoutnull的布局对象frm
 }
}

 -------------------------------------------------------------------------------------------------------

结果如下:

 

0 0
原创粉丝点击