1、2.3 使用布局管理器,主讲教师:董婷,一、知识能力目标,1. 理解容器的概念及层次; 2. 掌握常用布局管理器的使用; 3. 运用布局管理器设计布局美观的GUI程序,二、项目14 电子日历,显示2008年的电子日历,1.程序代码,import java.util.Calendar; import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class CalendarMainClass public static void main(String args)Cale
2、ndarFrame frame=new CalendarFrame();frame.setBounds(100,100,360,300); /设置窗体位置大小frame.setVisible(true); /设置窗体可见frame.validate();frame.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););,class CalendarBean/创建用于判断年月日之间关系的类CalendarBeanString day;int year=2008,m
3、onth=0;public void setYear(int year) /设置年份的方法this.year=year;public int getYear() /获得年份的方法return year;public void setMonth(int month) /设置月份的方法this.month=month;public int getMonth() /获得月份的方法return month;,public String getCalendar()String a=new String42;Calendar 日历=Calendar.getInstance();日历.set(year,mo
4、nth-1,1);int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;int day=0;if(month=1|month=3|month=5|month=7|month=8|month=10|month=12)day=31;if(month=4|month=6|month=9|month=11)day=30;if(month=2) /判断月份是否为2月if(year%4=0),elseday=28;for(int i=星期几,n=1;i星期几+day;i+)ai=String.valueOf(n);n+;return a; class CalendarFrame e
5、xtends Frame implements ActionListener/创建日历界面类CalendarFrameLabel labelDay=new Label42;Button titleName=new Button7;String name=“日“,“一“,“二“,“三“,“四“,“五“,“六“;Button nextMonth,previousMonth;int year=2008,month=2;CalendarBean calendar;Label showMessage=new Label(“,Label.CENTER);,public CalendarFrame()Pan
6、el pCenter=new Panel(); /创建pCenter面板pCenter.setLayout(new GridLayout(7,7); /将pCenter的布局设置为7行7列的GridLayout布局for(int i=0;i7;i+)titleNamei=new Button(namei);pCenter.add(titleNamei);for(int i=0;i42;i+)labelDayi=new Label(“,Label.CENTER);pCenter.add(labelDayi); calendar=new CalendarBean();calendar.setYea
7、r(year);calendar.setMonth(month);String day=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);,nextMonth=new Button(“下月“);previousMonth=new Button(“上月“);nextMonth.addActionListener(this);previousMonth.addActionListener(this);Panel pNorth=new Panel(),pSouth=new Panel();pNorth.add(prev
8、iousMonth);pNorth.add(nextMonth);pSouth.add(showMessage);showMessage.setText(“当前是:“+calendar.getYear()+“年“+calendar.getMonth()+“月“);ScrollPane scrollPane=new ScrollPane();scrollPane.add(pCenter);add(scrollPane,BorderLayout.CENTER); /在窗口中心区域添加scrollPaneadd(pNorth,BorderLayout.NORTH); /在窗口北面区域添加pNorth
9、add(pSouth,BorderLayout.SOUTH); /在窗口南面区域添加pSouth,public void actionPerformed(ActionEvent e)if(e.getSource()=nextMonth) /单击下月按钮时刷新日期month=month+1;if(month12)month=1;calendar.setMonth(month);String day=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);else if(e.getSource()=previousMont
10、h) /单击上月按钮时刷新日期month=month-1;if(month1)month=12;calendar.setMonth(month);String day=calendar.getCalendar();,for(int i=0;i42;i+)labelDayi.setText(dayi);showMessage.setText(“当前是:“+calendar.getYear()+“年“+calendar.getMonth()+“月“);,2.相关知识,流布局(FlowLayout) 呈水平放置,直到同一条线上再也没有适合的控件 对齐方式由 align 属性确定。可能的值为: LEF
11、T RIGHT CENTER LEADING TRAILING,边界布局(BorderLayout) 对容器组件进行安排,并调整其大小,使其符合下列五个区域:南、北、东、西和中间区域,网格布局(GridLayout)以矩形网格形式对容器的组件进行布置 容器被分成大小相等的矩形,一个矩形中放置一个组件,卡片布局(CardLayout)将容器中的每个组件看作一张卡片,三、项目15 使用网格袋布局,使用网格袋布局管理器,将窗体中的控件进行如图布局设计,1.程序代码,import java.awt.*; import java.awt.event.*; import javax.swing.*; cl
12、ass GBC extends GridBagConstraints private static final long serialVersionUID = 6657272964185905015L;/设置网格座标参数public GBC(int x, int y) this.gridx = x;this.gridy = y;/设置网格座标和占的网格数public GBC(int gridx, int gridy, int gridwidth, int gridheight) this.gridx = gridx;this.gridy = gridy;this.gridwidth = gri
13、dwidth;this.gridheight = gridheight;,public GBC setAnchor(int anchor) this.anchor = anchor;return this;public GBC setFill(int fill) this.fill = fill;return this;/设置单元网格大小public GBC setWeight(double weightx, double weighty) this.weightx = weightx;this.weighty = weighty;return this; /设置网格之间的相互距离public
14、 GBC setInset(int distance) this.insets = new Insets(distance, distance, distance, distance);return this;,/设置网格之间的相互距离public GBC setInset(int top, int left, int bottom, int right) this.insets = new Insets(top, left, bottom, right);return this;/设置网格的初始位置public GBC setIpad(int ipadx, int ipady) this.i
15、padx = ipadx;this.ipady = ipady;return this; public class GridBagLayoutTest extends JFrame /* 序列化序号private static final long serialVersionUID = 6763277356654198370L; public GridBagLayoutTest() this.setTitle(“GridBagLayoutTest”); / 窗体标题 this.setSize(400, 150); / 窗体大小,/ 设置窗体居中显示在屏幕上Dimension size = To
16、olkit.getDefaultToolkit().getScreenSize();this.setLocation(int) (size.getWidth() - this.getWidth() / 2,(int) (size.getHeight() - this.getHeight() / 2);/ 初始化窗口面板this.initial();/ 设置窗体为不可改变大小this.setResizable(false);/ 设置窗体为显示this.setVisible(true); /初始化窗体上的面板public void initial() JPanel panel = new JPan
17、el();panel.setLayout(new GridBagLayout(); / 设置所有控件所占网格的大小,位置,座标,对齐方式,panel.add(new JScrollPane(new JTextArea(), new GBC(0, 0, 2, 3).setWeight(10, 0).setFill(GBC.BOTH);panel.add(new JButton(“按钮一“), new GBC(2, 0, 2, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JButton(“按钮二“), new GBC(2, 1).setWe
18、ight(5, 0).setFill(GBC.BOTH);panel.add(new JButton(“按钮三“), new GBC(3, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JComboBox(new String “组合框“ ), new GBC(2, 2, 2, 1).setWeight(5, 0).setFill(GBC.BOTH);panel.add(new JTextField(“单行文本框“), new GBC(0, 3, 4, 1).setWeight(5, 0).setFill(GBC.BOTH);this.s
19、etLayout(new BorderLayout();this.getContentPane().add(panel, BorderLayout.CENTER);,public static void main(String args) / 匿名内部类,为窗口注册事件new GridBagLayoutTest().addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0);); ,2.相关知识 GridBagLayout类,GridBagConstraints类,f
20、ill NONE:不调整组件大小。 HORIZONTAL:加宽组件,使它在水平方向上得以填满其显示区域,但是不改变高度。 VERTICAL:加高组件,使它在垂直方向上得以填满其显示区域,但是不改变宽度。 BOTH:使组件完全填满其显示区域 anchor 可能的值有两种:相对和绝对 相对值的解释是相对于容器的组件方向属性 PAGE_START、PAGE_END、LINE_START、LINE_END、FIRST_LINE_START、FIRST_LINE_END、LAST_LINE_START 和 LAST_LINE_END。 绝对值则不然 CENTER、NORTH、NORTHEAST、EAST
21、、SOUTHEAST、SOUTH、SOUTHWEST、WEST 和 NORTHWEST 默认值为 CENTER,Insets 对象是容器边界的表示形式 它指定容器必须在其各个边缘留出的空间。 这个空间可以是边界、空白空间或标题。 Insets(int top, int left, int bottom, int right) 创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象。 默认值为 new Insets(0, 0, 0, 0)。,GridBagLayout 不要求组件的大小相同即可将组件垂直和水平对齐 每个 GridBagLayout 对象维持一个动态的矩形
22、单元网格,每个组件占用一个或多个这样的单元,称为显示区域。 每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联 Constraints 对象指定组件在网格中的显示区域以及组件在其显示区域中的放置方式 网格的总体方向取决于容器的 ComponentOrientation 属性 对于水平的从左到右的方向,网格坐标 (0,0) 位于容器的左上角,其中 X 向右递增,Y 向下递增 对于水平的从右到左的方向,网格坐标 (0,0) 位于容器的右上角,其中 X 向左递增,Y 向下递增。 为了有效使用网格包布局,必须自定义与组件相关联的一个或多个 GridBagConstraints 对象 可以通过设置一个或多个实例变量来自定义 GridBagConstraints,【应用拓展】,Java允许用手工布局(null)放置各个组件 需要自己设置组件的位置和大小 步骤: 取消容器的默认管理器:setLayout(null) 设置位置和大小 setLocation()、setSize()、setBounds()等,