温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java图形界面中常用的布局方式有哪些

发布时间:2022-02-21 16:27:04 来源:亿速云 阅读:159 作者:iii 栏目:开发技术

今天小编给大家分享一下Java图形界面中常用的布局方式有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

流式布局

采用流式布局会将元素按从左到右的顺序排列,如果一个元素在一行中放不下,那这个元素会另起一行依然按照从左到右的顺序排列

代码

public class Test {
	public static void main(String[] args) {
//		创建窗口
		JFrame jFrame = new JFrame();
//		设置窗口名称
		jFrame.setTitle("hello");
//		创建流式布局管理器 对齐方式为左对齐
		LayoutManager layout = new FlowLayout(FlowLayout.LEFT);
//		关闭窗口结束程序
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		创建内容面板
		Container contentpage = jFrame.getContentPane();
//		设置内容面板布局方式为流布局
		contentpage.setLayout(layout);
//		创建按钮
		JButton button1 = new JButton("1");
		JButton button2 = new JButton("2");
		JButton button3 = new JButton("3");
		JButton button4 = new JButton("4");
		JButton button5 = new JButton("5");
//		设置按钮大小
		button1.setPreferredSize(new Dimension(100,100));
		button2.setPreferredSize(new Dimension(100,100));
		button3.setPreferredSize(new Dimension(100,100));
		button4.setPreferredSize(new Dimension(100,100));
		button5.setPreferredSize(new Dimension(100,100));
//		设置按钮背景颜色
		button1.setBackground(Color.red);
		button2.setBackground(Color.blue);
		button3.setBackground(Color.pink);
		button4.setBackground(Color.orange);
		button5.setBackground(Color.yellow);
//		将按钮添加到内容面板中
		contentpage.add(button1);
		contentpage.add(button2);
		contentpage.add(button3);
		contentpage.add(button4);
		contentpage.add(button5);
//		设置窗口大小
		jFrame.setSize(500, 300);
//		设置窗口可见
		jFrame.setVisible(true);	
	}
}

边界布局

采用边界布局会将元素分别划分到东,西,中,南,北五个方位,分别使用EAST,WEST,CENTER,SOUTH,NORTH标识,每个方位只能放一个元素

代码

public class Test {
	public static void main(String[] args) {
//		创建窗口
		JFrame jFrame = new JFrame();
//		设置窗口名称
		jFrame.setTitle("hello");
//		创建边界布局管理器
		BorderLayout layout = new BorderLayout();
//		关闭窗口结束程序
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		创建内容面板
		Container contentpage = jFrame.getContentPane();
//		设置内容面板布局方式为流布局
		contentpage.setLayout(layout);
//		创建按钮
		JButton button1 = new JButton("1");
		JButton button2 = new JButton("2");
		JButton button3 = new JButton("3");
		JButton button4 = new JButton("4");
		JButton button5 = new JButton("5");
//		设置按钮背景颜色
		button1.setBackground(Color.red);
		button2.setBackground(Color.blue);
		button3.setBackground(Color.pink);
		button4.setBackground(Color.orange);
		button5.setBackground(Color.yellow);
//		将按钮添加到内容面板中
//		将按钮放置到北部
		contentpage.add(button1,BorderLayout.NORTH);
//		将按钮放置到南部
		contentpage.add(button2,BorderLayout.SOUTH);
//		将按钮放置到西部
		contentpage.add(button3,BorderLayout.WEST);
//		将按钮放置到东部
		contentpage.add(button4,BorderLayout.EAST);
//		将按钮放置到中心
		contentpage.add(button5,BorderLayout.CENTER);
//		设置窗口大小
		jFrame.setSize(500, 300);
//		设置窗口可见
		jFrame.setVisible(true);
	}
}

卡片布局

顾名思义,若一个容器使用卡片布局,其里面的所有组件就像是一副牌一样重叠在一起,容器只能显示一个组件,默认显示第一个组件,可以通过CardLayout中的show方法改变显示的组件

代码

public class Test {
	public static void main(String[] args) {
//			创建窗口
		JFrame jFrame = new JFrame();
//			设置窗口名称
		jFrame.setTitle("hello");
//			创建卡片布局管理器
		CardLayout layout = new CardLayout();
//			关闭窗口结束程序
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//			创建面板
		JPanel jPanel = new JPanel();
//			设置面板布局方式为卡片布局
		jPanel.setLayout(layout);
//			添加 按钮 设置背景颜色
		JButton jButton1 = new JButton();
		jButton1.setBackground(Color.pink);
		JButton jButton2 = new JButton();
		jButton2.setBackground(Color.yellow);
//			将按钮添加到面板中并对按钮进行命名
		jPanel.add(jButton1,"bt1");
		jPanel.add(jButton2,"bt2");
//			指定在面板上显示的按钮
		layout.show(jPanel, "bt2");
//			将面板添加到窗口中
		jFrame.add(jPanel);
//			设置窗口大小
		jFrame.setSize(500,300);
//			设置窗口可见
		jFrame.setVisible(true);
	}
}

自定义布局

所谓自定义布局就是不使用任何布局管理器,而是我们自己通过指定组件的X坐标,Y坐标,宽度,高度来指定组件的位置

组件是以左上角顶点为原点来定位坐标,使用自定义布局,要将容器使用的布局管理器设置为null

那有的小伙伴会问了,既然布局管理器设置为null,那可不可以直接不设置啊,当然不行,如果不设置的话,组件会不显示

代码

public class Test {
	public static void main(String[] args) {
//				创建窗口
		JFrame jFrame = new JFrame();
//				设置窗口名称
		jFrame.setTitle("hello");
//				关闭窗口同时结束程序
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//				创建面板
		JPanel jPanel = new JPanel();
//		使用自定义布局,将容器使用的布局管理器设置为null
		jPanel.setLayout(null);
//				添加 按钮 设置背景颜色
		JButton jButton1 = new JButton();
		jButton1.setBackground(Color.pink);
		JButton jButton2 = new JButton();
		jButton2.setBackground(Color.yellow);
//		设置按钮的坐标为(100,100) ,宽度为100,高度为100
		jButton1.setBounds(new Rectangle(100,100,100,100));
//		设置按钮的坐标为(220,70) ,宽度为100,高度为100
		jButton2.setBounds(new Rectangle(220,70,100,100));
//				将按钮添加到面板中
		jPanel.add(jButton1);
		jPanel.add(jButton2);
//				将面板添加到窗口中
		jFrame.add(jPanel);
//				设置窗口大小
		jFrame.setSize(500,300);
//				设置窗口可见
		jFrame.setVisible(true);
	}
}

以上就是“Java图形界面中常用的布局方式有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI