下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 代码

代码.doc

代码

djunpei
2011-05-24 0人阅读 举报 0 0 暂无简介

简介:本文档为《代码doc》,可适用于IT/计算机领域

.​ 创建一个Frame类型的窗口采用GridLayout布局依照Windows的简单计算器的样式在窗口中创建一个计算器的界面:代码如下:importjavaawt*publicclassExa{publicstaticvoidmain(Stringargs){FramemyFrame=newFrame("计算器")myFramesetBounds(,,,)myFramesetBackground(ColorGRAY)myFramesetResizable(false)myFramesetVisible(true)Menubianji=newMenu("编辑")Menuchakan=newMenu("查看")Menubangzhu=newMenu("帮助")MenuBarmenubar=newMenuBar()menubaradd(bianji)menubaradd(chakan)menubaradd(bangzhu)myFramesetMenuBar(menubar)myFramesetLayout(newGridLayout(,,,))Panela=newPanel()asetSize(,)asetLayout()TextFieldtext=newTextField("")textsetBounds(,,,)aadd(text)myFrameadd(a)Panelb=newPanel()bsetSize(,)bsetLayout()LabelLabel=newLabel()LabelsetBounds(,,,)LabelsetBackground(ColorWHITE)badd(Label)myFrameadd(b)Buttonbutton=newButton("Backspace")buttonsetForeground(ColorRED)buttonsetBounds(,,,)badd(button)myFrameadd(b)Buttonbutton=newButton("CE")buttonsetForeground(ColorRED)buttonsetBounds(,,,)badd(button)myFrameadd(b)Buttonbutton=newButton("C")buttonsetForeground(ColorRED)buttonsetBounds(,,,)badd(button)myFrameadd(b)Panelc=newPanel()csetSize(,)csetLayout()Buttonbutton=newButton("MC")buttonsetForeground(ColorRED)buttonsetBounds(,,,)cadd(button)myFrameadd(c)Buttonbutton=newButton("")buttonsetBounds(,,,)cadd(button)myFrameadd(c)Buttonbutton=newButton("")buttonsetBounds(,,,)cadd(button)myFrameadd(c)Buttonbutton=newButton("")buttonsetBounds(,,,)cadd(button)myFrameadd(c)Buttonbutton=newButton("")buttonsetBounds(,,,)cadd(button)myFrameadd(c)Buttonbutton=newButton("sqart")buttonsetForeground(ColorRED)buttonsetBounds(,,,)cadd(button)myFrameadd(c)Paneld=newPanel()dsetSize(,)dsetLayout()Buttonbutton=newButton("MR")buttonsetForeground(ColorRED)buttonsetBounds(,,,)dadd(button)myFrameadd(d)Buttonbutton=newButton("")buttonsetBounds(,,,)dadd(button)myFrameadd(d)Buttonbutton=newButton("")buttonsetBounds(,,,)dadd(button)myFrameadd(d)Buttonbutton=newButton("")buttonsetBounds(,,,)dadd(button)myFrameadd(d)Buttonbutton=newButton("*")buttonsetBounds(,,,)dadd(button)myFrameadd(d)Buttonbutton=newButton("")buttonsetForeground(ColorRED)buttonsetBounds(,,,)dadd(button)myFrameadd(d)Panele=newPanel()esetSize(,)esetLayout()Buttonbutton=newButton("MS")buttonsetForeground(ColorRED)buttonsetBounds(,,,)eadd(button)myFrameadd(e)Buttonbutton=newButton("")buttonsetBounds(,,,)eadd(button)myFrameadd(e)Buttonbutton=newButton("")buttonsetBounds(,,,)eadd(button)myFrameadd(e)Buttonbutton=newButton("")buttonsetBounds(,,,)eadd(button)myFrameadd(e)Buttonbutton=newButton("")buttonsetBounds(,,,)eadd(button)myFrameadd(e)Buttonbutton=newButton("x")buttonsetForeground(ColorRED)buttonsetBounds(,,,)eadd(button)myFrameadd(e)Panelf=newPanel()fsetSize(,)fsetLayout()Buttonbutton=newButton("M")buttonsetForeground(ColorRED)buttonsetBounds(,,,)fadd(button)myFrameadd(f)Buttonbutton=newButton("")buttonsetBounds(,,,)fadd(button)myFrameadd(f)Buttonbutton=newButton("")buttonsetBounds(,,,)fadd(button)myFrameadd(f)Buttonbutton=newButton("")buttonsetBounds(,,,)fadd(button)myFrameadd(f)Buttonbutton=newButton("")buttonsetBounds(,,,)fadd(button)myFrameadd(f)Buttonbutton=newButton("=")buttonsetForeground(ColorRED)buttonsetBounds(,,,)fadd(button)myFrameadd(f)myFramesetVisible(true)}}计算器的运行效果如下:.​ 创建一个Frame类型的窗口窗口中放置标签、文本域及按钮组件构造出密码验证的界面:代码如下:importjavaawt*publicclassExe{publicExe(Stringstring){}publicstaticvoidmain(Stringargs){Framea=newFrame()asetTitle("学生的个人基本信息")asetSize(,)asetLayout()LabelLabelName=newLabel("用户名")LabelNamesetBounds(,,,)TextFieldtextFieldName=newTextField()textFieldNamesetBounds(,,,)LabelLabelPass=newLabel("登陆密码")LabelPasssetBounds(,,,)TextFieldtextFieldPass=newTextField()textFieldPasssetEchoChar('*')textFieldPasssetBounds(,,,)ButtonbtnVerify=newButton("登陆")btnVerifysetBounds(,,,)aadd(LabelName)aadd(textFieldName)aadd(LabelPass)aadd(textFieldPass)aadd(btnVerify)asetLocation(,)asetVisible(true)}}用户登陆密码如下所示:.​ 编写一个菜单程序其中包含文本、格式、图片和动画菜单。其中文本、图片和动画菜单中分别包含显示文本、显示图片和播放动画菜单项格式菜单中包含字体大小、字体颜色两个菜单项字体大小菜单项又包含、、三个子菜单项字体颜色菜单项又包含红色、绿色、蓝色三个子菜单项。代码如下:importjavaawt*classExa{staticFramemyFrame=newFrame()privatefinalStringcolors={"红色","绿色","蓝色"}privatefinalStringsizes={"","",""}privatefinalStringpictures={"显示图片"}privatefinalStringTexts={"显示文本"}privatefinalStringdonghuas={"播放动画"}publicExa(Stringtitle){构造方法myFramesetTitle(title)Menuform=newMenu("格式")创建菜单对象formadd(newMenuItem("字体颜色"))formadd(newMenuItem("字体大小"))Menucolor=newMenu("字体颜色")for(intk=k<colorslengthk)coloradd(newMenuItem(colorsk))为菜单添加菜单项Menusize=newMenu("字体大小")for(intk=k<sizeslengthk)sizeadd(newMenuItem(sizesk))formaddSeparator()添加分隔条formadd(color)添加子菜单formadd(size)Menupicture=newMenu("图片")Menuxianshi=newMenu("显示图片")for(intk=k<pictureslengthk)pictureadd(newMenuItem(picturesk))MenuText=newMenu("文本")Menuxianshiwenben=newMenu("显示文本")for(intk=k<Textslengthk)Textadd(newMenuItem(Textsk))Menudonghua=newMenu("动画")Menubofangdonghua=newMenu("播放动画")for(intk=k<donghuaslengthk)donghuaadd(newMenuItem(donghuask))MenuBarmenubar=newMenuBar()创建菜单栏对象menubaradd(Text)将菜单对象添加到菜单栏menubaradd(picture)menubaradd(form)menubaradd(donghua)myFramesetMenuBar(menubar)}publicstaticvoidmain(Stringargs){ExaappFrame=newExa("MenuSystemDemo")创建主类的实例myFramesetBounds(,,,)myFramesetVisible(true)显示窗口}}运行结果如下:.​ 编写一个用户验证程序最多允许用户试验三次米板三次都不正确则关闭程序。代码如下:importjavaawt*importjavaawtevent*importjavaxswing*importjavaxswingevent*publicclassExaextendsJFrameimplementsActionListener{JLabeljLabelNameJTextFieldjtextFieldNameJLabeljLabelPassJPasswordFieldjtextFieldPassstaticintsumpublicExa(Stringstring){setBounds(,,,)setTitle("登陆")ContainercontentPane=getContentPane()contentPanesetLayout()jLabelName=newJLabel("用户名")jLabelNamesetBounds(,,,)jtextFieldName=newJTextField()jtextFieldNamesetBounds(,,,)jLabelPass=newJLabel("登陆密码")jLabelPasssetBounds(,,,)jtextFieldPass=newJPasswordField()jtextFieldPasssetEchoChar('*')jtextFieldPasssetBounds(,,,)JButtonbtnVerify=newJButton("登陆")btnVerifysetBounds(,,,)contentPaneadd(jLabelName)contentPaneadd(jtextFieldName)contentPaneadd(jLabelPass)contentPaneadd(jtextFieldPass)contentPaneadd(btnVerify)btnVerifyaddActionListener(this)setVisible(true)}.​ 利用列表框设计一个学生课程选项列表清单在清单的左侧是课程名称右侧为相应的课时数通过鼠标选择课程选项。单击确定按钮后弹出对话框显示用户所选课程和课时以及总课时如果要选择多门课程在单击选择课程时按下Shift键或Ctrl键。代码如下:importjavaawt*importjavaawtevent*importjavaxswing*importjavaxswingevent*publicclassExaextendsJFrameimplementsActionListener,ListSelectionListener{ContainercontentPaneJListlistJListlistJPanelPanel,PanelJButtonbtn,btnStringkecheng={"数学","英语","语文","计算机"}Stringkeshi={"","","",""}publicExa(Stringstring){setTitle("课程表")setBounds(,,,)contentPane=getContentPane()contentPanesetLayout(newGridLayout(,,,))Panel=newJPanel()PanelsetLayout()PanelsetBackground(ColorGRAY)PanelsetSize(,)btn=newJButton("确定")btnsetBounds(,,,)list=newJList(kecheng)listsetBounds(,,,)btnaddActionListener(this)listaddListSelectionListener(this)Paneladd(btn)Paneladd(list)Panel=newJPanel()PanelsetLayout()PanelsetBackground(ColorGRAY)PanelsetSize(,)btn=newJButton("退出")btnsetBounds(,,,)list=newJList(keshi)listsetBounds(,,,)btnaddActionListener(this)listaddListSelectionListener(this)Paneladd(list)Paneladd(btn)contentPaneadd(Panel)contentPaneadd(Panel)setVisible(true)}publicstaticvoidmain(Stringargs){newExa("kehcnegbiao")}publicvoidactionPerformed(ActionEvente){TODOAutogeneratedmethodstubif(egetSource()==btn){Stringstr="课程课时n"intsum=for(inti=i<listgetSelectedValues()lengthi){str=listgetSelectedValues()i""listgetSelectedValues()i"n"sum=IntegerparseInt(listgetSelectedValues()itoString())}str="总课时为:"sumJOptionPaneshowMessageDialog(this,str)}if(egetSource()==btn){Systemexit()}}publicvoidvalueChanged(ListSelectionEvente){if(egetSource()==list){listsetSelectedIndex(listgetSelectedIndex())}if(egetSource()==list){listsetSelectedIndex(listgetSelectedIndex())}}}.使用Swing组件完成如下题目:建立一个窗口内容为:输入学生姓名(JTEXTFIELD)年龄(JTEXTFIELD)。在屏幕上显示学生照片(照片随便找图片用JLABEL)。放“确定”和“退出”按钮。点击确定弹出窗口显示学生的姓名和年龄。点击退出结束程序 在上一程序上改进:增加学生的兴趣爱好:多选。至少五个爱好。增加学生性别选择:单选按钮点击确定难就:则在对话框中能显示学生的基本信息、爱好和他的性别。代码如下:importjavaawt*importjavaawtevent*importjavaxswing*publicclassExaextendsJFrameimplementsActionListener{ContainercontentPaneJPanelPanel,Panel,Panel,PanelJLabellabel,label,PictureJTextFieldName,AgeJRadioButtonradio,radioButtonGroupsexJCheckBoxa,a,a,a,a,a,a,aJButtonbtn,btnpublicExa(Stringstring){setTitle("学生个人信息")setBounds(,,,)contentPane=getContentPane()contentPanesetLayout(newGridLayout(,))Panel=newJPanel()PanelsetLayout(newBoxLayout(Panel,BoxLayoutYAXIS))label=newJLabel("学生姓名")label=newJLabel("年龄")Name=newJTextField()Age=newJTextField()Picture=newJLabel()PicturesetSize(,)PicturesetIcon(newImageIcon("iconjpg"))contentPaneadd(Picture)Paneladd(label)Paneladd(Name)Paneladd(label)Paneladd(Age)contentPaneadd(Panel)Panel=newJPanel()PanelsetLayout(newBoxLayout(Panel,BoxLayoutYAXIS))a=newJCheckBox("喜欢吃零食")a=newJCheckBox("喜欢饭后散步")a=newJCheckBox("喜欢看小说")a=newJCheckBox("喜欢睡觉")a=newJCheckBox("喜欢玩电脑")a=newJCheckBox("喜欢吃水果")a=newJCheckBox("最喜欢玩炫舞")a=newJCheckBox("喜欢晒太阳")Paneladd(a)Paneladd(a)Paneladd(a)Paneladd(a)Paneladd(a)Paneladd(a)Paneladd(a)Paneladd(a)contentPaneadd(Panel)Panel=newJPanel()radio=newJRadioButton("男",false)radio=newJRadioButton("女",true)sex=newButtonGroup()sexadd(radio)sexadd(radio)Paneladd(radio)Paneladd(radio)contentPaneadd(Panel)Panel=newJPanel()PanelsetLayout()btn=newJButton("确定")btnsetBounds(,,,)btn=newJButton("退出")btnsetBounds(,,,)Paneladd(btn)Paneladd(btn)contentPaneadd(Panel)btnaddActionListener(this)btnaddActionListener(this)thisaddWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){Systemexit()}})}publicstaticvoidmain(Stringargs){Exaapp=newExa("学生个人信息")appsetVisible(true)}publicvoidactionPerformed(ActionEvente){if(egetSource()==btn){Strings=NamegetText()"n"AgegetText()"n"if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(aisSelected()){s=agetText()"n"}if(radioisSelected()){s=radiogetText()"n"}if(radioisSelected()){s=radiogetText()"n"}JOptionPaneshowMessageDialog(,s)}else{Systemexit()}}}第二部分面向对象编程题目:.创建一个矩形类Rectangle:定义私有数据成员Length和Width保存矩形的长和宽。定义方法getLength()、setLength()和getWidth()、setWidth()读写矩形的长和宽提供两个构造函数:无参构造函数将长和宽都初始化为。另一个两个参数构造函数初始化长和宽为指定的值提供三个方法:Perimeter计算矩形周长Area计算矩形面积show方法返回以下格式的字符串:“矩形长=xx,宽=xx”其中xx为具体的值代码如下:publicclassRectangle{privatedoublelengthprivatedoublewidthpublicdoublegetLength(){returnlength}publicvoidsetLength(doublelength){thislength=length}publicdoublegetWidth(){returnwidth}publicvoidsetWidth(doublewidth){thiswidth=width}publicRectangle(){length=width=}publicRectangle(doublelength,doublewidth){thislength=lengththiswidth=width}publicdoublePerimeter(){return(lengthwidth)*}publicdoubleArea(){returnlength*width}publicStringshow(){return"矩形的长="length",""矩形的宽="width}}编写主程序测试你写好的类:创建两个矩形对象(长宽分别为,和,)然后计算并显示他们的长、宽、周长和面积的值代码如下:publicclasstextRectangle{publicstaticvoidmain(Stringargs){RectangleR=newRectangle()RsetLength()RsetWidth()Systemoutprintln(Rshow())Systemoutprintln("矩形的周长="RPerimeter())Systemoutprintln("矩形的面积="RArea())Systemoutprintln("***********R***********")RectangleR=newRectangle()RsetLength()RsetWidth()Systemoutprintln(Rshow())Systemoutprintln("矩形的周长="RPerimeter())Systemoutprintln("矩形的面积="RArea())Systemoutprintln("***********R***********")}}运行结果如下所示:、从上题的Rectangle类派生立方体类Cube:  该类应有属性Length、Width和Height编写读写立方体的长、宽和高(注意长、宽和高的值不能为负数)  提供两个构造函数:  无参构造函数将长、宽和高都初始化为。  另一个构造函数有三个参数初始化长、宽和高为指定的值并调用超类的带两个参数的构造函数。  提供三个方法:  覆盖Area计算立方体的表面积  Volume计算立方体的体积。  覆盖show方法返回以下格式的字符串:“长=xx,宽=xx,高=xx”其中xx为具体的值。代码如下:publicclassCubeextendsRectangle{privatedoubleheigthpublicCube(){heigth=}publicdoubleArea(){return((getLength()*getWidth()getLength()*heigthgetWidth()*heigth))*}publicdoubleVolume(){returngetLength()*getWidth()*heigth}publicvoidCubeshow(){}publicStringshow(){return"矩形的长="getLength()",""矩形的宽="getWidth()",""矩形的高="heigth}publicstaticvoidmain(Stringargs){Cubea=newCube()asetLength()asetWidth()asetHeigth()Systemoutprintln(ashow())Systemoutprintln("矩形的表面积="aArea())Systemoutprintln("矩形的体积="aVolume())}publicdoublegetHeigth(){returnheigth}publicvoidsetHeigth(doubleheigth){thisheigth=heigth}}运行结果如下表所示:、编写抽象类Animal要求有如下成员) 两个普通属性Age(年龄)和weight(重量)) 编写get、set方法用于设定和读取Age和weight属性。) 抽象方法Shout用于描述该类型动物的叫声      抽象方法Type用于描述动物种类) 抽象方法TellAboutSelf用于将该动物的各属性特征输出代码如下:packageAnimalbaseimportAnimalRunnerpublicabstractclassAnimalimplementsRunner{privateintageprivateintweightpublicintgetAge(){returnage}publicvoidsetAge(intage){thisage=age}publicintgetWeight(){returnweight}publicvoidsetWeight(intweight){thisweight=weight}publicabstractStringshout()publicabstractStringType()publicabstractStringTellAboutSelf()}) 派生类Cat和Dog并使用实例对派生类进行测试。猫的实例化代码如下:packageAnimalsubimportAnimalbaseAnimalpublicclassCatextendsAnimal{publicStringTellAboutSelf(){return"颜色白色,爱吃鱼"}publicStringType(){return"cat"}publicStringshout(){return"miao~"}publicstaticvoidmain(Stringargs){Cata=newCat()asetAge()asetWeight()Systemoutprintln(agetAge())Systemoutprintln(agetWeight())Systemoutprintln(aType())Systemoutprintln(ashout())Systemoutprintln(aTellAboutSelf())}publicStringshowRunner(){return"活泼可爱"}}运行结果如下所示:狗的实例化代码如下:packageAnimalsubimportAnimalbaseAnimalpublicclassDogextendsAnimal{publicStringshout(){return"wangwang~"}publicStringTellAboutSelf(){return"活泼可爱"}publicStringType(){return"dog"}publicstaticvoidmain(Stringargs){DogD=newDog()DsetAge()DsetWeight()Systemoutprintln(DgetAge())Systemoutprintln(DgetWeight())Systemoutprintln(DType())Systemoutprintln(Dshout())Systemoutprintln(DTellAboutSelf())}publicStringshowRunner(){return"张牙舞爪"}}运行结果如下表所示:、改写第三题要求如下  )添加接口Runner存放在包Animal中要求该接口有抽象方法showRunner()由于描述行走方式       InterfaceRunner         {            abstractStringshowRunner()         }  )要求改写抽象类Animal存放在包Animalbase中并实现接口Runner  )要求改写派生类Cat和Dog存放在包Animalsub中并实现接口Runner中的方法ShowRunner代码如下:packageAnimalpublicinterfaceRunner{abstractStringshowRunner()}答案如下表所示:Java面试题:编程题(Thread和Runnable)【考题题干】创建两个线程每个线程打印出线程名字后再睡眠给其他线程以执行的机会每个线程前后共睡眠次。最后打印出线程结束信息退出。要求分别采用从Thread中继承和实现Runnable接口两种方式来实现程序(即写两个程序)。【参考答案】()继承Thread类实现方式publicclassBBextendsThread{      inttime      publicBB(intt){                    time=t      }      publicvoidrun(){          for(inti=i<=i){                Systemoutprintln(ThreadcurrentThread()getName()"  "i"次")                try{                      Threadsleep(time)                }catch(InterruptedExceptione){                      eprintStackTrace()                }                if(i==){                      Systemoutprintln(ThreadcurrentThread()getName()"退出了")                }          }            }      publicstaticvoidmain(Stringargs){          BBb=newBB()          bsetName("线程")          bstart()          BBb=newBB()          bsetName("线程")          bstart()          BBb=newBB()          bsetName("线程")          bstart()                }}()Runnable接口实现方式publicclassBBimplementsRunnable{      privateinttime      privateThreadth      publicBB(Stringname,intn){                    time=n          th=newThread(this,name)          thstart()      }      publicvoidrun(){          for(inti=i<=i){                Systemoutprintln(ThreadcurrentThread()getName()"  "i"次")                try{                      Threadsleep(time)                }catch(InterruptedExceptione){                      eprintStackTrace()                }                if(i==){                      Systemoutprintln(ThreadcurrentThread()getName()"退出了")                }          }            }publicstaticvoidmain(Stringargs){          BBb=newBB("线程",)                    BBb=newBB("线程",)          BBb=newBB("线程",)                                    }}    

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/23

代码

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利