布局大全

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
/*
 Container是面板  所有的界面都必须要包含这个东西才能显示出来  像下面的getContentPane()是这个类里面的 就是把组件先添加到面板上面
 然后再显示出来,这个往往是无形的用
 JPanel 也是一种面板容器类  所以可以直接往上面增加组件   默认:FlowLayout(流式布局)
 JFrame 它是屏幕上window的对象   也就是他实例化就是这个窗口  默认:BorderLayout(边界布局)
 */
public class test extends JFrame {
	JPanel p = new JPanel();
	// BorderLayout && FlowLayout
	/*
	public test() {
		setLayout(new BorderLayout(5, 5));//俩个参数分别是组件的间隔高和宽
		setFont(new Font("Helvetica", Font.PLAIN, 14));
		getContentPane().add("North", new JButton("North"));
		getContentPane().add("South", new JButton("South"));
		getContentPane().add("East", new JButton("East"));
		getContentPane().add("West", new JButton("West"));
		// 设置面板为流式布局居中显示,组件横、纵间距为5个像素
		p.setLayout(new FlowLayout(1, 5, 5));  //流式布局  第一个值是说明这个布局是直线  align
		
		for (int i = 1; i < 10; i++) {
			// String.valueOf(i),将数字转换为字符串
			JButton b = new JButton(String.valueOf(i));
			p.add(b); // 将按钮添加到面板中
		}
		getContentPane().add("Center", p); // 将面板添加到中间位置
	}
	*/
	
	// BoxLayout
    /*
	public test() {
		BoxLayout layout=new BoxLayout(p, BoxLayout.X_AXIS);//第一个参数是关联的面板容器  第二个是表示水平方向
		p.setLayout(layout);
		setContentPane(p);//记住这里也要把小的容器放到Container面板上面去
		
		String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" };
		String[][] rowData = { { "张三", "男", "计算机系", "100 米 ,200 米", "" },
				{ "李四", "男", "化学系", "100 米,铅球", "" }, };
		// 创建表格
		JTable table = new JTable(new DefaultTableModel(rowData, columnName));
		// 创建包含表格的滚动窗格
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// 定义 p 的布局为 BoxLayout,BoxLayout 为垂直排列
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		// 先加入一个不可见的 Strut,从而使 topPanel 对顶部留出一定的空间
		p.add(Box.createVerticalStrut(10));
		// 加入包含表格的滚动窗格
		p.add(scrollPane);
		// 再加入一个不可见的 Strut,从而使 p 和 middlePanel 之间留出一定的空间
		p.add(Box.createVerticalStrut(10));
	}
	*/
	
	// GridLayout
	/*
	public test() {
		
		setLayout(new GridLayout(3, 2,10,10));//默认 和行列  和行列水平垂直距离三种构造函数
		setFont(new Font("Helvetica", Font.PLAIN, 14));
		getContentPane().add(new JButton("Button 1,1"));
		getContentPane().add(new JButton("Button 1,2"));
		getContentPane().add(new JButton("Button 2,1"));
		getContentPane().add(new JButton("Button 2,2"));
		getContentPane().add(new JButton("Button 3,1"));
		LayoutManager.removeLayoutComponent(Component("Button 1,2"));
		
		 函数汇总
		 int getColumns(); getRows();getHgap();getVgap();  获取对应的上面的四个参数
		 void removeLayoutComponent(Component comp) :从布局移除指定组件。
         void getColumns(int Columns); getRows(int Rows);getHgap(int Hgap);getVgap(int Vgap); 
                     把上面的四个参数设定为制定的值
         String toString():返回此网格布局的值的字符串表示形式。
		 
	}
	*/
	
	public static void main(String args[]) {
		test f = new test();//JFame的一个类的对象    通过这个来调用面板显示出来
		f.setTitle("边界布局");
		f.pack(); // Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLocationRelativeTo(null); // the window is placed in the center of the screen
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值