Java GUI编程(5)- 3种布局管理器
2021/11/2 9:10:22
本文主要是介绍Java GUI编程(5)- 3种布局管理器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
流式布局
1 public class TestFlowLayout { 2 public static void main(String[] args) { 3 Frame frame = new Frame(); 4 5 //组件-按钮 6 Button button1 = new Button("button01"); 7 Button button2 = new Button("button02"); 8 Button button3 = new Button("button03"); 9 10 //设置成流式布局 11 frame.setLayout(new FlowLayout());//居中 12 frame.setLayout(new FlowLayout(FlowLayout.LEFT));//居左 13 frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//居右 14 15 frame.setSize(200,200); 16 frame.add(button1); 17 frame.add(button2); 18 frame.add(button3); 19 20 frame.setVisible(true); 21 22 frame.addWindowListener(new WindowAdapter() { 23 @Override 24 public void windowClosing(WindowEvent e) { 25 System.exit(0); 26 } 27 }); 28 } 29 }
东西南北中布局
1 //东西南北中布局 2 public class TestBorderLayout { 3 public static void main(String[] args) { 4 Frame frame = new Frame(); 5 6 Button east = new Button("east"); 7 Button west = new Button("west"); 8 Button south = new Button("south"); 9 Button north = new Button("north"); 10 Button centor = new Button("centor"); 11 12 frame.setSize(400,400); 13 14 frame.add(east,BorderLayout.EAST); //东 15 frame.add(west,BorderLayout.WEST); //西 16 frame.add(south,BorderLayout.SOUTH);//南 17 frame.add(north,BorderLayout.NORTH);//北 18 frame.add(centor,BorderLayout.CENTER);//中 19 20 frame.setVisible(true); 21 22 frame.addWindowListener(new WindowAdapter() { 23 @Override 24 public void windowClosing(WindowEvent e) { 25 System.exit(0); 26 } 27 }); 28 } 29 }
表格式布局
1 //表格式布局 2 public class TestGridLayout { 3 public static void main(String[] args) { 4 Frame frame = new Frame(); 5 6 Button button1 = new Button("button1"); 7 Button button2 = new Button("button2"); 8 Button button3 = new Button("button3"); 9 Button button4 = new Button("button4"); 10 Button button5 = new Button("button5"); 11 Button button6 = new Button("button6"); 12 13 frame.setSize(500,500); 14 15 frame.setLayout(new GridLayout(3,2)); //三行两列 16 17 frame.add(button1); 18 frame.add(button2); 19 frame.add(button3); 20 frame.add(button4); 21 frame.add(button5); 22 frame.add(button6); 23 24 frame.setVisible(true); 25 26 frame.addWindowListener(new WindowAdapter() { 27 @Override 28 public void windowClosing(WindowEvent e) { 29 System.exit(0); 30 } 31 }); 32 } 33 }
练习
1 public class TestHomeWork { 2 public static void main(String[] args) { 3 4 //总frame,一分为二,上下两部分 5 Frame frame = new Frame(); 6 frame.setSize(500,400); 7 frame.setVisible(true); 8 frame.setLayout(new GridLayout(2,1)); 9 10 //上下两个panel面板 11 Panel panel1 = new Panel(); 12 panel1.setBackground(new Color(246, 142, 175)); 13 panel1.setLayout(new BorderLayout()); 14 Panel panel2 = new Panel(new BorderLayout()); 15 panel2.setBackground(new Color(71, 196, 214)); 16 17 frame.add(panel1); 18 frame.add(panel2); 19 20 //上面的面板 21 panel1.add(new Button("01-east"),BorderLayout.EAST); 22 panel1.add(new Button("01-west"),BorderLayout.WEST); 23 24 Panel panel3 = new Panel(); 25 panel3.setLayout(new GridLayout(2,1)); 26 panel3.add(new Button("01-01")); 27 panel3.add(new Button("01-02")); 28 panel1.add(panel3,BorderLayout.CENTER); 29 30 //下面的面板 31 panel2.add(new Button("02-east"),BorderLayout.EAST); 32 panel2.add(new Button("02-west"),BorderLayout.WEST); 33 34 Panel panel4 = new Panel(new GridLayout(2,2)); 35 for (int i = 0; i <4 ; i++) { 36 panel4.add(new Button("02-"+i)); 37 } 38 39 panel2.add(panel4); 40 41 frame.addWindowListener(new WindowAdapter() { 42 @Override 43 public void windowClosing(WindowEvent e) { 44 System.exit(0); 45 } 46 }); 47 } 48 }
总结
- frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器种
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小、定位、背景颜色、可见性
这篇关于Java GUI编程(5)- 3种布局管理器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现