Java之GUI(图形界面编程)布局管理器

BorderLayout边框布局:将容器分为东、西、南、北、中5个部分

Java之GUI(图形界面编程)布局管理器_第1张图片

ex:

/**
 * BorderLayout边框布局:将容器分为东、西、南、北、中5个部分
 * @author Administrator
 */
public class BorderLayoutDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
		frame.setVisible(true);//显示窗口
		frame.setSize(600, 400);//设置窗口大小
		frame.setLocationRelativeTo(null);//设置窗口居中
		frame.setDefaultCloseOperation(3);//点击X关闭程序  3为结束程序
		
		Button button1 = new Button("按钮北");//创建一个按钮对象
		Button button2 = new Button("按钮南");//创建一个按钮对象
		Button button3 = new Button("按钮东");//创建一个按钮对象
		Button button4 = new Button("按钮西");//创建一个按钮对象
		Button button5 = new Button("中");//创建一个按钮对象
		button1.setBackground(Color.BLUE);
		button2.setBackground(Color.green);
		button3.setBackground(Color.magenta);
		button4.setBackground(Color.cyan);
		button5.setBackground(Color.orange);
		button1.setSize(frame.getWidth(), 150);
		frame.add(button1,BorderLayout.NORTH);//添加对象到窗口上,指定位置北
		frame.add(button2,BorderLayout.SOUTH);//添加对象到窗口上,指定位置南
		frame.add(button3,BorderLayout.EAST);//添加对象到窗口上,指定位置东
		frame.add(button4,BorderLayout.WEST);//添加对象到窗口上,指定位置西
		frame.add(button5,BorderLayout.CENTER);//添加对象到窗口上,指定位置中     center也是边框布局默认设置位置 		
	}

}

FlowLayout流式布局:默认将所有的组件放第一排  如果当前排放不下则自动换行放在第二排

Java之GUI(图形界面编程)布局管理器_第2张图片

ex:

/*
 * FlowLayout流式布局:默认将所有的组件放第一排  如果当前排放不下则自动换行放在第二排
 */
public class FlowLayoutDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
		frame.setVisible(true);//显示窗口
		frame.setSize(600, 400);//设置窗口大小
		frame.setLocationRelativeTo(null);//设置窗口居中
		frame.setDefaultCloseOperation(3);//点击X关闭程序  3为结束程序
		
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));//设置为流布局   居左对齐
		
		for (int i = 0; i < 15; i++) {
			Button button = new Button("按钮"+(i+1));
			button.setBackground(Color.orange);
			frame.add(button);
		}	
	}
	
}

GridLayout网格布局 : 分成指定行和列 添加的每一个组件占一格

Java之GUI(图形界面编程)布局管理器_第3张图片

ex:

/**
 * GridLayout网格布局 : 分成指定行和列 添加的每一个组件占一格
 * @author Administrator
 */
public class GridLayoutDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
		frame.setVisible(true);//显示窗口
		frame.setSize(600, 400);//设置窗口大小
		frame.setLocationRelativeTo(null);//设置窗口居中
		frame.setDefaultCloseOperation(3);//点击X关闭程序  3为结束程序
		
		frame.setLayout(new GridLayout(3,3,10,10));//设置3行3列 间距10
		
		for (int i = 0; i < 9; i++) {
			Button button = new Button("按钮"+(i+1));
			button.setBackground(Color.orange);
			frame.add(button);
		}
	}

}

自定义布局(即使用绝对位置布局):

Java之GUI(图形界面编程)布局管理器_第4张图片

ex:

/**
 * 自定义布局
 * 清空布局 setLayout(null)
 * 使用绝对位置  setBounds( x坐标,y坐标,宽,高 )  
 * add方法添加组件到窗口上
 * @author Administrator
 */
public class MyLayout {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
		frame.setLayout(null);//清空布局
		
		frame.setVisible(true);//显示窗口
		frame.setSize(600, 400);//设置窗口大小
		frame.setLocationRelativeTo(null);//设置窗口居中
		frame.setDefaultCloseOperation(3);//点击X关闭程序  3为结束程序
		
		Button button = new Button("按钮");
		button.setBounds(200,100,200,200);//设置组件位置大小
		button.setBackground(Color.ORANGE);
		frame.add(button);
	}

}

 

你可能感兴趣的:(-----❶,JavaSE基础)