《使用布局管理器》PPT课件

上传人:资****亨 文档编号:153568137 上传时间:2020-11-30 格式:PPT 页数:31 大小:272.50KB
返回 下载 相关 举报
《使用布局管理器》PPT课件_第1页
第1页 / 共31页
《使用布局管理器》PPT课件_第2页
第2页 / 共31页
《使用布局管理器》PPT课件_第3页
第3页 / 共31页
《使用布局管理器》PPT课件_第4页
第4页 / 共31页
《使用布局管理器》PPT课件_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《《使用布局管理器》PPT课件》由会员分享,可在线阅读,更多相关《《使用布局管理器》PPT课件(31页珍藏版)》请在金锄头文库上搜索。

1、管理课件,1,2.3 使用布局管理器,主讲教师:董婷,管理课件,2,一、知识能力目标,1. 理解容器的概念及层次; 2. 掌握常用布局管理器的使用; 3. 运用布局管理器设计布局美观的GUI程序,管理课件,3,二、项目14 电子日历,显示2008年的电子日历,管理课件,4,1.程序代码,import java.util.Calendar; import java.util.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class CalendarMainClass public stati

2、c void main(String args) CalendarFrame 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); ); ,管理课件,5,class CalendarBean/创建用于判断年月日

3、之间关系的类CalendarBean String day; int year=2008,month=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; ,管理课件,6,public String getCalendar() String a

4、=new String42; Calendar 日历=Calendar.getInstance(); 日历.set(year,month-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) ,管理课件,7,else day=28; fo

5、r(int i=星期几,n=1;i星期几+day;i+) ai=String.valueOf(n); n+; return a; class CalendarFrame extends Frame implements ActionListener/创建日历界面类CalendarFrame Label labelDay=new Label42; Button titleName=new Button7; String name=日,一,二,三,四,五,六; Button nextMonth,previousMonth; int year=2008,month=2; CalendarBean c

6、alendar; Label showMessage=new Label(,Label.CENTER);,管理课件,8,public CalendarFrame() Panel 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=n

7、ew Label(,Label.CENTER); pCenter.add(labelDayi); calendar=new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); String day=calendar.getCalendar(); for(int i=0;i42;i+) labelDayi.setText(dayi);,管理课件,9,nextMonth=new Button(下月); previousMonth=new Button(上月); nextMonth.addActionListener(t

8、his); previousMonth.addActionListener(this); Panel pNorth=new Panel(),pSouth=new Panel(); pNorth.add(previousMonth); pNorth.add(nextMonth); pSouth.add(showMessage); showMessage.setText(当前是:+calendar.getYear()+年+calendar.getMonth()+月); ScrollPane scrollPane=new ScrollPane(); scrollPane.add(pCenter);

9、add(scrollPane,BorderLayout.CENTER); /在窗口中心区域添加scrollPane add(pNorth,BorderLayout.NORTH); /在窗口北面区域添加pNorth add(pSouth,BorderLayout.SOUTH); /在窗口南面区域添加pSouth ,管理课件,10,public void actionPerformed(ActionEvent e) if(e.getSource()=nextMonth) /单击下月按钮时刷新日期 month=month+1; if(month12) month=1; calendar.setMon

10、th(month); String day=calendar.getCalendar(); for(int i=0;i42;i+) labelDayi.setText(dayi); else if(e.getSource()=previousMonth) /单击上月按钮时刷新日期 month=month-1; if(month1) month=12; calendar.setMonth(month); String day=calendar.getCalendar();,管理课件,11,for(int i=0;i42;i+) labelDayi.setText(dayi); showMessa

11、ge.setText(当前是:+calendar.getYear()+年+calendar.getMonth()+月); ,管理课件,12,2.相关知识,流布局(FlowLayout) 呈水平放置,直到同一条线上再也没有适合的控件 对齐方式由 align 属性确定。可能的值为: LEFT RIGHT CENTER LEADING TRAILING,管理课件,13,管理课件,14,边界布局(BorderLayout) 对容器组件进行安排,并调整其大小,使其符合下列五个区域:南、北、东、西和中间区域,管理课件,15,网格布局(GridLayout)以矩形网格形式对容器的组件进行布置 容器被分成大小

12、相等的矩形,一个矩形中放置一个组件,管理课件,16,卡片布局(CardLayout)将容器中的每个组件看作一张卡片,管理课件,17,三、项目15 使用网格袋布局,使用网格袋布局管理器,将窗体中的控件进行如图布局设计,管理课件,18,1.程序代码,import java.awt.*; import java.awt.event.*; import javax.swing.*; class GBC extends GridBagConstraints private static final long serialVersionUID = 6657272964185905015L; /设置网格座标

13、参数 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 = gridwidth; this.gridheight = gridheight;,管理课件,19,public GBC setAnchor(int anchor) this.anchor = anchor; r

14、eturn 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 GBC setInset(int distance) this.insets = new Insets(distance, distance, distance, di

15、stance); return this;,管理课件,20,/设置网格之间的相互距离 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.ipadx = 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); / 窗体大小,管理课件,21,/ 设置窗体居中显示在屏幕上 Dimension size = Toolkit.getDefaultToolkit().getScreenSize

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号