湖南科技职业学院软件职业技术学院 教师课时授课计划 教师姓名: 授课班级: 授课课时:6 课程名称:JAVA程序设计 第 4 课 教学循环号:4-1 课题 使用布局管理器  各教学环节课时分配 讲授  4 课时   有指导的实践  2 课时   独立实践  0 课时  教学内容 在用户接口上组织控件  重点   难点 难点:CardLayout与GridBagLayout,尤其是GridBagLayout的GridBagConstraints的七个重要属性  教具 多媒体  作业 独立实践  授课日期 (校历) 第 3 周星期 1 第 1-4 节   第 3 周星期 2 第 5-6 节   第 周星期 第 节   第 周星期 第 节  课后小结 通过本课的学习,学生掌握了: 理解布局管理器 掌握几种常用的布局管理器 FlowLayout, GridLayout, BorderLayout, CardLayout,BoxLayout, GridBagLayout  详细教学过程附后 详细教学过程: 教学目标陈述(5分钟) 在用户接口上组织组件 了解和使用各种布局管理器(45*2分钟) 了解布局管理器: 布局管理器是确定容器中的组件的大小和位置的过程 它们是执行布局管理的特殊的对象 Java 提供布局管理器类如:FlowLayout, GridLayout, BorderLayout, CardLayout, 及 GridBagLayout 布局管理器类是从Object 类扩展过来的 布局管理器类都在java.awt 包中提供 流布局 applet的缺省管理器 例如: public class SampleLayout extends JApplet { JButton b1,b2,b3; FlowLayout f1; public void init() { f1=new FlowLayout(FlowLayout.LEFT); JPanel p1=new JPanel(); getContentPane().add(p1); p1.setLayout(f1); b1=new JButton(“Button 1”); b2=new JButton(“Button 2”); b3=new JButton(“Button 3”); p1.add(b1); p1.add(b2); p1.add(b3); } } 按钮左对齐流布局:  网格布局 把显示区编组为矩形格子 例子: public class SampleLayout extends JApplet { JButton b1,b2,b3,b4; GridLayout g1; public void init() { g1=new GridLayout(2,2); JPanel p1=new JPanel(); getContentPane().add(p1); p1.setLayout(g1); b1=new JButton(“Button 1”); b2=new JButton(“Button 2”); b3=new JButton(“Button 3”); b4=new JButton(“Button 4”); p1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); } } 用网格布局组织的按钮:  边界布局 让你按方向来安置组件 例子: public class SampleLayout extends JApplet { JButton b1,b2,b3,b4,b5; BorderLayout bd1; public void init() { bd1=new BorderLayout(); JPanel p1=new JPanel(); getContentPane().add(p1); p1.setLayout(bd1); b1=new JButton(“Button 1”); b2=new JButton(“Button 2”); b3=new JButton(“Button 3”); b4=new JButton(“Button 4”); b5=new JButton(“Button 5”); p1.add(“North”,b1); p1.add(“South”,b2); p1.add(“East”,b3); p1.add(“West”,b4); p1.add(“Center”,b5); } } 用边界布局组织的按钮:  卡片布局 用来创建以一个跟着另一个的布局放置的布局堆 例子: public class SampleLayout extends JApplet { JButton button1,button2,button3; CardLayout c1; JPanel p1; public void init() { p1 = new JPanel(); c1 = new CardLayout(); p1.setLayout(c1); getContentPane().add(p1); button1 = new JButton(“Button 1”); button2 = new JButton(“Button 2”); button3 = new JButton(“Button 3”); p1.add(“Button 1”, button1); p1.add(“Button 2”, button2); p1.add(“Button 3”, button3); } } 用卡片布局组织的按钮:  盒布局 用它来创建一堆布局,可在容器中水平或垂直地安排布局 例子: import java.applet.*; import javax.swing.*; public class SampleLayout extends JApplet { JPanel calpanel,logpanel; JPanel mainpanel;? GridLayout gridObj; GridBagLayout gridbagObj; GridBagConstraints gbc; BoxLayout boxObj; ? //Set grid layout for calculator panel gridObj=new GridLayout(8,2); JPanel calpanel=new JPanel(); calpanel.setLayout(gridObj); ? //Set gridbag layout for login panel gridbagObj = new GridBagLayout(); gbc = new GridBagConstraints(); logpanel = new JPanel(); logpanel.setLayout(gridbagObj); //Set box layout for the mail panel mainpanel=new JPanel(); boxObj=new BoxLayout (mainpanel,BoxLayout.X_AXIS); ? /* Specify the names for the panels that will be added to the box layout */ boxObj.addLayoutComponent("Calculator Panel",calpanel); boxObj.addLayoutComponent("Text Panel",logpanel); ? //Add the main panel to the applet getContentPane().add(mainpanel); /* Add the child panels(calculator panel and login panel) to the main panel */ mainpanel.add(calpanel); mainpanel.add(logpanel); /* Initialize the controls and them to the appropriate panels */ } } 水平放置的二个面板的盒布局  GridBag布局 用于创建布局堆 GridBagLayout 类用来创建gridbag布局 GridBagConstraints 类用来设置按gridbag布局放置的组件的位置和大小约束 三、实例分析 4.D.1 (45*2分钟) 1、问题陈述(组织控件) 2.D.1中已创建的接受客户材料的用户界面不是组织得很好。这样来组织这些组件使得所有的标号控件和数据入口控件出现在分离的列上。 客户数据录入表 2、确定任务 识别所需的布局 识别控件 的位置 识别变量名 布局管理器应用到容器 加入控件到applet 保存、编译、及执行程序 3、分析解决问题 识别要用的布局 结果: 要用布局是 gridbag 布局,因它是最灵活的布局,允许组件放置在容器内的任何行和列位置上 识别控件的位置 指出约束,用GridBagConstraint 类的以下属性: gridwidth, gridheight –指出组件的显示区域的宽和高 anchor –为在它的显示区域内确定组件的位置 fill – 指出如何使组件填入它的显示区域 gridx, gridy – 指出长方形网格中组件的位置 weightx, weighty – 指出applet 显示区域中组件如何填入 关于 weightx 和 weighty的例子:对于Button 1 fill的值必须设置为 GridBagConstraints.NONE  结果: 变量 gridx Gridy  labelCustName textCustName 1 4 5 5  labelCustCellNo textCustCellNo 1 4 8 8  labelCustPackage comboCustPackage 1 4 11 11  labelCustAge textCustAge 1 4 14 14   识别变量名 结果: 名为 gbObject的变量表示GridBagLayout类的对象 名为gbc 的变量表示 GridBagConstraints 类的对象 应用布局管理器于容器 动作: public class Customer extends JApplet { static JPanel panelObject; GridBagLayout gbObject; GridBagConstraints gbc; public void init() { gbObject = new GridBagLayout(); gbc = new GridBagConstraints(); panelObject = (JPanel)getContentPane(); panelObject.setLayout(gbObject); } } 4、执行验证 把控件加入到applet 保存、编译、及执行此程序 结果: 验证此屏幕布局是否与提供的示例屏幕匹配 四、指导性练习 4.P.1(45*2分钟) 1、问题陈述(组织控件) 你需创建一个用户界面,显示客户服务执行的性能单,按所示的组件编组。 2、问题分析 3、验证 五、小结 (10分钟)