首页 > 编程知识 正文

为什么要使用布局管理器,jpanel缺省的布局管理器

时间:2023-05-04 09:30:28 阅读:110181 作者:4784

流布局管理器FlowLayout

------------- -请参阅

特点:

1、组件按照添加的优先顺序从左到右排列

2、一行满了自动移至下一行

3、每行组件的默认设置居中

4、流布局管理器是JPanel的缺省布局管理器

构建方法:

1、FlowLayout () )

创建溢出管理器,其中容器中的对象居中对齐,对象的默认边距为5PX

2、flowlayout(intalign ) )。

使用指定的对齐方式创建相同的布局管理器

3、flowlayout(intalign,int hrap,int vgap ) )。

创建具有相同功能的管理器,但对象的水平间距为hgap垂直间距为vgap

主要成员数据

1、FlowLayout.LEFT

每行的组件左对齐

2、FlowLayout.CENTER

每行组件的居中对齐

3、FlowLayout.RIGHT

每行的组件右对齐

一般方法

1、int getHgap (,voidsethgap )、inthgap )。

返回到和在布局管理器中设置组件的水平间距

2、int getVgap (,voidsetvgap )、intvgap )。

返回到和在布局管理器中设置组件的垂直间距

3、void getAlignment (,setalignment )、intalign ) )。

设置和布局管理器的对齐方式

4、voidlayoutcontainer (container target ) )。

将target容器组件的页面布局方法设置为FlowLayout

5、voidaddlayoutcomponent (string name,Component comp ) )。

将指定的组件添加到布局管理器中并指定名称

6、voidremovelayoutcomponent (component comp ) )。

从布局管理器中删除指定的组件

小实例

public class FlowLayout_demo

{

静态jframe frame=new jframe (流布局管理器);

publicstaticvoidmain (string [ ] args ) )。

{

flowlayout flow=new flowlayout (flowlayout.center,5,10 );

JButton button=new JButton ('按钮);

JLabel label=new JLabel ('我是标签) );

frame.setlayout(flow;

frame.setsize (500,400 );

frame.set location (200,100 );

frame.add(Button;

frame.add(label;

frame.add(newjtextfield ('这里是文本中毒) );

frame.setdefaultcloseoperation (jframe.exit _ on _ close );

frame.setvisible(true;

}

}

边界布局管理器BorderLayou

------------- -请参阅

特点:

1 )按区域将整个布局分为东西南北两部分

2 .向布局中添加组件时,必须指定要添加到布局中的哪个区域

3 )如果在添加组件时未指定区域,则缺省情况下会将其添加到中间区域

4 )如果原始区域中包含组件,并在其中添加组件,则会覆盖原始组件

5、边界布局管理器是JFrame、JAapplet和JDialog的缺省布局管理器

构建方法

1、BorderLayout (

创建容器中每个对象之间没有间隔的布局管理器

2、borderlayout(intHGAP,int vgap ) ) )。

创建布局管理器,其中容器中每个对象之间的水平间距为hgap,垂直间距为vgap

一般方法

1、int getHgap (,voidsethgap )、inthgap )。

返回,设置布局管理器中每个对象的水平间隔

2、int getVgap (,void s

etHgap(int hgap)

返回,设置布局管理器中各个对象的垂直间距

3,void addLayoutComponent(String name,Component comp)

将指定的组件添加到布局管理器中,并且制定名称

4,void removeLayoutComponent(Component comp)

将指定的组件从布局管理器中移除

主要成员数据

1,BorderLayout.EAST

将组件放在容器右边

2,BorderLayout.WEST

将组件放在容器的左边

3,BorderLayout.SOUTH

将组件放在容器的下边

4,BorderLayout.NORTH

将组件放在容器的上边

5,BorderLayout.CENTER

将组件放在容器的中间

小实例

public class BorderLayout_demo

{

static JFrame frame = new JFrame("边界布局管理器");

public static void main(String[] args)

{

BorderLayout border = new BorderLayout(5,10);

frame.setLayout(border);

frame.setSize(300,200);

frame.setLocation(200,100);

frame.add(new JButton("上"),BorderLayout.NORTH);

frame.add(new JButton("下"),BorderLayout.SOUTH);

frame.add(new JButton("左"),BorderLayout.WEST);

frame.add(new JButton("右"),BorderLayout.EAST);

frame.add(new JButton("中"),BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

网格布局管理器 GridLayout

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

特点

1,将空间会分为若干个行与列的网格形式,每个网格成为单元格

2,添加组件的时候,会按照从左到右,从上到下的顺序一次添加

3,网格的行数和列数在创建网格的时候指定

4,容器中的每个组件占用一个单元格,所以组件的大小是一致的

5,当容器大小变化是,组件的大小随着改变,但是相对位置不变

构造方法

1,GridLayout()

创建一个网格布局管理器,默认每行只有一个组件

2,Grid Layout(int rows, int cols)

创建一个网格布局管理器,并制定行和列

3,GridLayout(int rows, int cols, int hgap, int vgap)

创建一个指定行和列的管理器,并设置水平和垂直间距

常用方法

1,void setRows(int rows)

设置布局管理器的行数

2,void setColumns(int cols)

设置布局管理器的列数

3,void setHgap(int hgap),void setVgap(int vgap)

设置组件之间的水平和垂直间距

4,void removeLayoutComponent(Component comp)

将指定的组件从布局中删除

小实例

public class GridLayout_demo extends JFrame{

static JPanel pan = new JPanel();

static JTextField text = new JTextField("0. ");

static JButton[] buttonArr = new JButton[10];

static JButton ba,bb,bc,bd,be,bf;

public static void main(String [] args)

{

GridLayout_demo frame = new GridLayout_demo();

for (int i = 0; i <= 9; i ++)

{

buttonArr[i] = new JButton("" + i);

}

ba = new JButton(".");bb = new JButton("+");bd = new JButton("-");bd = new JButton("*");be = new JButton("/");bf = new JButton("=");

frame.setTitle("网格布局管理器");

frame.setLayout(null);

frame.setSize(500,400);

frame.setLocation(200,100);

frame.setResizable(false);

GridLayout grid = new GridLayout(4,4);

pan.setLayout(grid);

frame.add(pan);

pan.add(buttonArr[7]);pan.add(buttonArr[8]);pan.add(buttonArr[9]);

pan.add(ba);

pan.add(buttonArr[4]);pan.add(buttonArr[5]);pan.add(buttonArr[6]);

pan.add(bb);

pan.add(buttonArr[1]);pan.add(buttonArr[2]);pan.add(buttonArr[3]);

pan.add(bc);pan.add(bd);pan.add(be);pan.add(bf);

text.setBounds(30,10,100,60);

pan.add(text);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。