布局 java swing_java中Swing五种常见的布局方式

1、 边界布局(BorderLayout)

2、流式布局(FlowLayout)

3、网格布局(GridLayout)

4、盒子布局(BoxLaYout)

5、空布局(null)

还有其他两种布局,分别是GridBagLayout(网格包布局)、CardLayout(卡片布局)

注意:JFrame和JDialog默认布局为BorderLayout,JPanel和Applet默认布局为FlowLayout

边界布局示例代码:

import java.awt.BorderLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class BorderLayoutExample extends JFrame{

JButton btn1=new JButton("东");

JButton btn2=new JButton("南");

JButton btn3=new JButton("西");

JButton btn4=new JButton("北");

JButton btn5=new JButton("中");

BorderLayoutExample(){

init();

this.setTitle("边界布局");

this.setResizable(true);

this.setSize(200, 200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

void init(){

this.setLayout(new BorderLayout(10,5)); //默认为0,0;水平间距10,垂直间距5

this.add(btn1,BorderLayout.EAST);

this.add(btn2,BorderLayout.SOUTH);

this.add(btn3,BorderLayout.WEST);

this.add(btn4,BorderLayout.NORTH);

this.add(btn5,BorderLayout.CENTER);

}

public static void main(String args[]){

new Bor

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值