




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java程序设计教案(第五章)教案纸 第5章 Java的图形用户界面(6学时)【主要讲授内容及时间分配】5.1 图形用户界面概述(25分钟)5.2 AWT中常用类的层次结构(20分钟)5.3 基本组件的使用(45分钟)5.4 布局管理器(45分钟)5.5 事件处理 (90分钟)5.6 菜单、绘图类的使用(45分钟)【重点与难点】1、重点:(1)基本组件的使用,包括Label、Button、TextField、TextArea、List、Checkbox和CheckboxGroup的构造方法和常用方法的使用。(2)布局管理器的使用,包括FlowLayout、GridLayout、BorderLa
2、yout。(3)事件处理。2、难点:事件处理。【教学要求】1、可以构造出图形用户界面;2、可以为界面上的组件编写相应的事件处理代码;3、可以构造带有菜单的应用程序;4、可以写小游戏。【实施方法】课堂讲授,PPT配合5.3 基本组件的使用1 Label类import java.awt.*;public class LabelDemo extends Framepublic LabelDemo()Label lb1,lb2,lb3;lb1=new Label(LeftLabel);lb2=new Label(CenterLabel,Label.CENTER);lb3=new Label(Right
3、Label,Label.RIGHT);lb3.setText(改变我的名字);lb3.setAlignment(Label.CENTER);/lb3.setVisible(false);setLayout(new FlowLayout();add(lb1);add(lb2);add(lb3); public static void main(String args) LabelDemo Ld=new LabelDemo(); Ld.setVisible(true); Ld.pack(); 2 Button类import java.awt.*;public class MyButtons pub
4、lic static void main(String args) Frame f = new Frame(); f.setLayout(new FlowLayout(); Button button1 = new Button(Ok); Button button2 = new Button(Open); Button button3 = new Button(Close); f.add(button1); f.add(button2); f.add(button3); f.setSize(300,100); f.setVisible(true); 3 CheckBox 类import ja
5、va.awt.*;public class CheckboxDemo2 extends Frame public CheckboxDemo2() setLayout(new FlowLayout(); String University=Tsinghua,Pecking,Fudan,Nanki,Tianjin; Checkbox c=new Checkbox5; Label label=new Label(There are 5 University!); Label label2=new Label(There are 5 University!); Label label3=new Lab
6、el(There are 5 University!); add(new Label(Please choice the University:);for(int i=0;i5;i+)ci=new Checkbox(Universityi);add(ci);add(label);add(label2);add(label3);this.pack(); show();public static void main(String args)CheckboxDemo2 cbd=new CheckboxDemo2();cbd.setSize(400,500);4 CheckBoxGroup类impor
7、t java.awt.*;import java.awt.CheckboxGroup;public class CheckboxGroupDemo2 extends Frame public CheckboxGroupDemo2() super(CheckboxGroupDemo2); setLayout(new FlowLayout(); String University=Tsinghua,Pecking,Fudan,Nanki,Tianjin; Checkbox c=new Checkbox5; Label label=new Label(There are 5 University!)
8、;CheckboxGroup d=new CheckboxGroup();add(new Label(Please choice the University:);for(int i=0;i=0) handleDel(list.getSelectedIndex(); else if(e.getSource()=up)&(list.getSelectedIndex()0) handleIncPriority(list.getSelectedIndex(); else if(e.getSource()=down)&(list.getSelectedIndex()=0) handleDecPrior
9、ity(list.getSelectedIndex(); else if(e.getSource()=list) taskInput.setText(list.getSelectedItem(); taskInput.requestFocus(); private void handleAdd(String newTask) list.add(newTask); list.select(list.getItemCount()-1); taskInput.setText(); private void handleDel(int pos) list.remove(pos); list.selec
10、t(pos); private void handleIncPriority(int pos) String item=list.getItem(pos); list.remove(pos); list.add(item,pos-1); list.select(pos-1); private void handleDecPriority(int pos) if(poscage.getSize().width) x=cage.getSize().width; xdir*=(-1); if(xcage.getSize().height) y=cage.getSize().height; ydir*
11、=(-1); if(y0) ydir*=-1; public void draw(Graphics g) g.drawImage(bird,x,y,30,30,cage); Cage类import java.awt.*;import java.awt.event.*;public class Cage extends Frame implements ActionListener private Button quit=new Button(Quit); private Button start=new Button(Start); private Button stop=new Button
12、(Stop); private Bird birds=new Bird20; Image bird=Toolkit.getDefaultToolkit().getImage(qq.jpg); public Cage() super(Cage with Birds); setLayout(new FlowLayout(); add(quit); quit.addActionListener(this); add(start); start.addActionListener(this); add(stop); stop.addActionListener(this); setSize(400,5
13、00); this.setVisible(true); for(int i=0;ibirds.length;i+) int x=(int)(getSize().width*Math.random(); int y=(int)(getSize().height*Math.random(); birdsi=new Bird(this,x,y); public void actionPerformed(ActionEvent e) if(e.getSource()=stop) for(int i=0;ibirds.length;i+) birdsi.halt(); if(e.getSource()=
14、start) for(int i=0;ibirds.length;i+) birdsi.halt(); birdsi=new Bird(this,birdsi.x,birdsi.y); if(e.getSource()=quit) System.exit(0); public void paint(Graphics g) for(int i=0;ibirds.length;i+) if(birdsi!=null) birdsi.draw(g); public static void main(String args) Cage table=new Cage(); DrawWithCanvasi
15、mport java.applet.*;import java.awt.*;public class DrawWithCanvas extends Frame private Button draw=new Button(Draw); private DrawCanvas canvas =new DrawCanvas(); public DrawWithCanvas() Panel panel=new Panel(); panel.setLayout(new FlowLayout(); panel.add(draw); setLayout(new BorderLayout(); add(Nor
16、th,panel); add(South,canvas); this.setSize(400,500); pack(); show(); public static void main(String args) DrawWithCanvas dnc=new DrawWithCanvas(); class DrawCanvas extends Canvas public DrawCanvas()this.setSize(400,500); public void paint(Graphics g) for(int i=12;igetSize().height;i+=12) g.drawString(y location is:+i,10,i); DrawNoCanvasimport java.applet.*;import java.awt.*;public class DrawNoCanvas extends Frame private Button draw=new Button(Draw); public DrawNoCanvas() Panel panel=new Panel(); panel.setLayout(new FlowLay
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年贵州省存量房买卖合同
- 2025标准办公楼租赁合同范本模板
- 2025商场物业管理合同示范文本
- 2025标准工业厂房租赁合同模板
- 2025茶叶店装修设计合同协议书范本
- 2025营养品代理合同书
- (三模)2025年5月潍坊市高三高考模拟考试语文试卷(含答案)
- 静脉输液避光护理安全操作规范
- 护理基础护理知识
- 断指康复护理
- 2025-2030年中国煤电行业市场深度发展趋势与前景展望战略研究报告
- 2025年医院感染控制试题及答案
- 2025年黄山市祁门文化旅游发展集团有限公司招聘5人笔试参考题库附带答案详解
- 2025-2030中国植物蛋白行业市场发展趋势与前景展望战略研究报告
- 环境保护项目进度安排与保障措施
- 2025年内蒙古自治区赤峰市通辽市中考二模历史试题(含答案)
- 马工程西方经济学(精要本第三版)教案
- 公路工程标准施工招标文件(2018年版)
- (正式版)SH∕T 3548-2024 石油化工涂料防腐蚀工程施工及验收规范
- 波纹管压浆料计算公式表
- 《质量管理体系文件》成品检验报告(COA)
评论
0/150
提交评论