欢迎来到天天文库
浏览记录
ID:35342116
大小:88.56 KB
页数:5页
时间:2019-03-23
《实训八gui编程练习》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实训八GUI编程练习(二)实训性质:验证性、程序设计实训目的(1)掌握事件监听机制(2)掌握ActionEvent事件的处理(3)掌握TextEvent事件的处理实训环境WindowXP/Sever2003>JDK>Eclipse实训内容(1)英语单词拼写训练(2)简易计算器界面(3)操作按钮移动实训指导L英语单词拼写训练⑴编写Java程序,窗口中有一个TextField对-象和一个按钮对象,将这两个对象添加到一个面板中,然后再添加到窗口北面。用户在TextFeild对象中输入一个单词,M车或者单机按钮,程序将创建若干个标签,其个数刚好等于单词字母个数,月.每
2、个标签上的字母刚好是单词的一个字母,但顺序不对。要求将这些标签添加到一个而板中,然后将该面板添加到窗口中心。用户用鼠标单击一个标签后,通过按下键盘上的上、下、左、右键,交换相邻标签上的字母,使得标签的字母顺序与输入单词的顺序相同。程序模板如下:RondomString.javapublicclassRondomString{Stringstr="";publicStringgetRondomString(Strings){StringBufferstrBuffer=newStringBuffer(s);intm^strBuffcr・length。;for(in
3、tk=0;k4、er{LetterLabel(){【代码1】//将当前对象注册为自身的焦点视器【代码2】〃将当前对象注册为自身的标监视器setBackground(Color・cyan);setFont(newFontFont.BOLD,30));}publicstaticLetterLabel[]gctLcttcrLabcl(intn){LetterLcibela[]=newLetterLabel[n];for(intk=0;k5、Evente){setBackground(Color・red);}publicvoidfocusLost(FocusEvcntc){setBackground(Color・cyan);}publicvoidmousePressed(MouseEvente){requestFocusO;}publicvoidsetText(charc){setLabel("+c);publicpublicvoidmouscEntcred(MouseEvente){}publicvoidmouseExited(MouseEvente){)voidmouseReleased(Mo6、useEveme){}publicvoidmousedicked(MouseEvente){}SpellingWordFrame.javaimportjava・awt・*;importjava・awt.event.*;importjavax・swing・Box;publicclassSpel1ingWordFrameextendsFrameimplementsKeyListenor,ActionListener{TextFieldinputWord;Buttonbutton;LetterLcibellabel[];PanelnorthP,centerP;Box7、wordBox;StringhintMessage二"用鼠标单击字母,按左右箭头交换字母,将其排列成所输入的单词";Isabelmessaagel.abe1=newIsabel(hintMessage);Stringword二"";SpcllingWordFramc0{inputWord二newTextEield(12);button=newButton("确定");button.addActionIJstenor(this);inputWord・addActionListener(this);northP=ncwPanel();northP.add(newL8、abel(z,输入一个英文单词:"))
4、er{LetterLabel(){【代码1】//将当前对象注册为自身的焦点视器【代码2】〃将当前对象注册为自身的标监视器setBackground(Color・cyan);setFont(newFontFont.BOLD,30));}publicstaticLetterLabel[]gctLcttcrLabcl(intn){LetterLcibela[]=newLetterLabel[n];for(intk=0;k5、Evente){setBackground(Color・red);}publicvoidfocusLost(FocusEvcntc){setBackground(Color・cyan);}publicvoidmousePressed(MouseEvente){requestFocusO;}publicvoidsetText(charc){setLabel("+c);publicpublicvoidmouscEntcred(MouseEvente){}publicvoidmouseExited(MouseEvente){)voidmouseReleased(Mo6、useEveme){}publicvoidmousedicked(MouseEvente){}SpellingWordFrame.javaimportjava・awt・*;importjava・awt.event.*;importjavax・swing・Box;publicclassSpel1ingWordFrameextendsFrameimplementsKeyListenor,ActionListener{TextFieldinputWord;Buttonbutton;LetterLcibellabel[];PanelnorthP,centerP;Box7、wordBox;StringhintMessage二"用鼠标单击字母,按左右箭头交换字母,将其排列成所输入的单词";Isabelmessaagel.abe1=newIsabel(hintMessage);Stringword二"";SpcllingWordFramc0{inputWord二newTextEield(12);button=newButton("确定");button.addActionIJstenor(this);inputWord・addActionListener(this);northP=ncwPanel();northP.add(newL8、abel(z,输入一个英文单词:"))
5、Evente){setBackground(Color・red);}publicvoidfocusLost(FocusEvcntc){setBackground(Color・cyan);}publicvoidmousePressed(MouseEvente){requestFocusO;}publicvoidsetText(charc){setLabel("+c);publicpublicvoidmouscEntcred(MouseEvente){}publicvoidmouseExited(MouseEvente){)voidmouseReleased(Mo
6、useEveme){}publicvoidmousedicked(MouseEvente){}SpellingWordFrame.javaimportjava・awt・*;importjava・awt.event.*;importjavax・swing・Box;publicclassSpel1ingWordFrameextendsFrameimplementsKeyListenor,ActionListener{TextFieldinputWord;Buttonbutton;LetterLcibellabel[];PanelnorthP,centerP;Box
7、wordBox;StringhintMessage二"用鼠标单击字母,按左右箭头交换字母,将其排列成所输入的单词";Isabelmessaagel.abe1=newIsabel(hintMessage);Stringword二"";SpcllingWordFramc0{inputWord二newTextEield(12);button=newButton("确定");button.addActionIJstenor(this);inputWord・addActionListener(this);northP=ncwPanel();northP.add(newL
8、abel(z,输入一个英文单词:"))
此文档下载收益归作者所有