java swing 页面布局方式_swing学习5--界面常见布局方式

本文介绍了Java Swing中的四种基本布局管理器:边界布局、流式布局、表格布局和盒子布局。通过示例代码展示了如何使用这些布局管理器创建不同效果的用户界面,包括按钮的定位和间隔设置。
摘要由CSDN通过智能技术生成

package swing;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class Test3 {

public static void main(String[] args) {

new Box1().setVisible(true);

}

}

class Border extends JFrame{

public Border(){

super("边界布局");

this.setSize(300,300);

this.setLocation(500,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建窗体可自由变化大小

this.setResizable(true);

//设置布局格式位边界布局,水品间距10,垂直间距5

this.setLayout(new BorderLayout(10,5));

JButton jb1 =new JButton("东");

JButton jb2 =new JButton("西");

JButton jb3 =new JButton("南");

JButton jb4 =new JButton("北");

JButton jb5 =new JButton("中");

this.add(jb1,BorderLayout.EAST);

this.add(jb2,BorderLayout.SOUTH);

this.add(jb3,BorderLayout.WEST);

this.add(jb4,BorderLayout.NORTH);

this.add(jb5,BorderLayout.CENTER);

}

}

class Flow extends JFrame{

public Flow(){

super("流式布局");

this.setSize(200,200);

this.setLocation(500,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建窗体可自由变化大小

this.setResizable(true);

//设置流式布局,默认为居中,设置靠左,水平间距10,垂直间距5

this.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));

JButton jb1 =new JButton("one");

JButton jb2 =new JButton("two");

JButton jb3 =new JButton("three");

JButton jb4 =new JButton("four");

JButton jb5 =new JButton("five");

this.add(jb1);

this.add(jb2);

this.add(jb3);

this.add(jb4);

this.add(jb5);

}

}

class Table extends JFrame{

public Table(){

super("表格布局");

this.setSize(500,200);

this.setLocation(500,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建窗体可自由变化大小

this.setResizable(true);

//设置表格布局,默认1行n列,设置2行3列,水平间距10,垂直间距5

this.setLayout(new GridLayout(2,3,10,5));

JButton jb1 =new JButton("one");

JButton jb2 =new JButton("two");

JButton jb3 =new JButton("three");

JButton jb4 =new JButton("four");

JButton jb5 =new JButton("five");

this.add(jb1);

this.add(jb2);

this.add(jb3);

this.add(jb4);

this.add(jb5);

}

}

class Box1 extends JFrame{

public Box1(){

super("盒子布局");

this.setSize(500,200);

this.setLocation(500,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建窗体可自由变化大小

this.setResizable(true);

//设置盒子布局,

this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.LINE_AXIS));

JButton jb1 =new JButton("one");

JButton jb2 =new JButton("two");

JButton jb3 =new JButton("three");

JButton jb4 =new JButton("four");

JButton jb5 =new JButton("five");

this.add(jb1);

this.add(jb2);

//采用X布局时,添加宽度组件隔开

this.getContentPane().add(Box.createHorizontalStrut(10));

this.add(jb3);

this.add(jb4);

//采用Y布局时,添加高度组件隔开

this.getContentPane().add(Box.createVerticalStrut(20));

this.add(jb5);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值