![實訓(xùn)八GUI編程練習(xí)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/d6f957c2-3598-4545-a73f-85aae71e82a7/d6f957c2-3598-4545-a73f-85aae71e82a71.gif)
![實訓(xùn)八GUI編程練習(xí)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/d6f957c2-3598-4545-a73f-85aae71e82a7/d6f957c2-3598-4545-a73f-85aae71e82a72.gif)
![實訓(xùn)八GUI編程練習(xí)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/d6f957c2-3598-4545-a73f-85aae71e82a7/d6f957c2-3598-4545-a73f-85aae71e82a73.gif)
![實訓(xùn)八GUI編程練習(xí)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/d6f957c2-3598-4545-a73f-85aae71e82a7/d6f957c2-3598-4545-a73f-85aae71e82a74.gif)
![實訓(xùn)八GUI編程練習(xí)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/d6f957c2-3598-4545-a73f-85aae71e82a7/d6f957c2-3598-4545-a73f-85aae71e82a75.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實訓(xùn)八 GUI編程練習(xí)(二)實訓(xùn)性質(zhì):驗證性、程序設(shè)計 實訓(xùn)目的(1)掌握事件監(jiān)聽機制(2)掌握ActionEvent事件的處理(3)掌握TextEvent事件的處理實訓(xùn)環(huán)境Window XP/Sever 2003、JDK、Eclipse 實訓(xùn)內(nèi)容(1)英語單詞拼寫訓(xùn)練 (2)簡易計算器界面(3)操作按鈕移動實訓(xùn)指導(dǎo)1. 英語單詞拼寫訓(xùn)練(1)編寫Java程序,窗口中有一個TextField對象和一個按鈕對象,將這兩個對象添加到一個面板中,然后再添加到窗口北面。用戶在TextFeild對象中輸入一個單詞,回車或者單機按鈕,程序?qū)?chuàng)建若干個標(biāo)簽,其個數(shù)剛好等于單詞字母個數(shù),且每個標(biāo)簽上的字母剛好
2、是單詞的一個字母,但順序不對。要求將這些標(biāo)簽添加到一個面板中,然后將該面板添加到窗口中心。用戶用鼠標(biāo)單擊一個標(biāo)簽后,通過按下鍵盤上的上、下、左、右鍵,交換相鄰標(biāo)簽上的字母,使得標(biāo)簽的字母順序與輸入單詞的順序相同。程序模板如下:RondomString.javapublic class RondomString String str="" public String getRondomString(String s) StringBuffer strBuffer=new StringBuffer(s); int m=strBuffer.length(); for(int k=
3、0;k<m;k+) int index=(int)(Math.random()*strBuffer.length(); char c=strBuffer.charAt(index); str=str+c; strBuffer=strBuffer.deleteCharAt(index); return str; LetterLabel.javaimport java.awt.*;import java.awt.event.*;public class LetterLabel extends Button implements FocusListener,MouseListener Lett
4、erLabel() 【代碼1】 /將當(dāng)前對象注冊為自身的焦點視器 【代碼2】 /將當(dāng)前對象注冊為自身的標(biāo)監(jiān)視器 setBackground(Color.cyan); setFont(new Font("",Font.BOLD,30); public static LetterLabel getLetterLabel(int n) LetterLabel a=new LetterLabeln; for(int k=0;k<a.length;k+) ak=new LetterLabel(); return a; public void focusGained(FocusE
5、vent e) setBackground(Color.red); public void focusLost(FocusEvent e) setBackground(Color.cyan); public void mousePressed(MouseEvent e) requestFocus(); public void setText(char c) setLabel(""+c); public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mou
6、seExited(MouseEvent e) public void mouseClicked(MouseEvent e)SpellingWordFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.Box;public class SpellingWordFrame extends Frame implements KeyListener,ActionListener TextField inputWord; Button button; LetterLabel label; Panel northP,c
7、enterP; Box wordBox; String hintMessage="用鼠標(biāo)單擊字母,按左右箭頭交換字母,將其排列成所輸入的單詞" Label messaageLabel=new Label(hintMessage); String word="" SpellingWordFrame() inputWord=new TextField(12); button=new Button("確定"); button.addActionListener(this); inputWord.addActionListener(this)
8、; northP=new Panel(); northP.add(new Label("輸入一個英文單詞:"); northP.add(inputWord); northP.add(button); centerP=new Panel(); wordBox=Box.createHorizontalBox(); centerP.add(wordBox); add(northP,BorderLayout.NORTH); add(centerP,BorderLayout.CENTER); add(messaageLabel,BorderLayout.SOUTH); setBoun
9、ds(100,100,350,180); setVisible(true); validate(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) word=inputWord.getText(); int n=word.length(); RondomString rondom=new RondomString(); String randomWord=ron
10、dom.getRondomString(word); wordBox.removeAll(); messaageLabel.setText(hintMessage); if(n>0) label=LetterLabel.getLetterLabel(n); for(int k=0;k<label.length;k+) 【代碼3】 /將當(dāng)前窗口注冊為labelk的鍵盤監(jiān)視器 labelk.setText(""+randomWord.charAt(k); wordBox.add(labelk); validate(); inputWord.setText(null)
11、; label0.requestFocus(); public void keyPressed(KeyEvent e) LetterLabel sourceLabel=(LetterLabel)e.getSource(); int index=-1; if(【代碼4】) /判斷按下的是否是鍵) for(int k=0;k<label.length;k+) if(labelk=sourceLabel) index=k; break; if(index!=0) String temp=labelindex.getText(); labelindex.setText(labelindex-1.
12、getText(); labelindex-1.setText(temp);labelindex-1.requestFocus(); else if(【代碼5】) /判斷按下的是否是鍵 for(int k=0;k<label.length;k+) if(labelk=sourceLabel) index=k; break; if(index!=label.length-1) String temp=labelindex.getText(); labelindex.setText(labelindex+1.getText(); labelindex+1.setText(temp);labe
13、lindex+1.requestFocus(); validate(); public void keyTyped(KeyEvent e) public void keyReleased(KeyEvent e) String success="" for(int k=0;k<label.length;k+) String str=labelk.getText(); success=success+str; if(success.equals(word) messaageLabel.setText("恭喜你,你成功了"); for(int k=0;k<label.length;k+) labelk.removeKeyListener(this);labelk.removeFocusListener(labelk); labelk.setBackground(Color.green); inputWord.requestFocus(); WordMainClass.j
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度長沙新環(huán)境房屋租賃與節(jié)能改造合同
- 2025年度辦公室助理實習(xí)生實習(xí)期間權(quán)益保護合同
- 家具買賣合同
- 農(nóng)業(yè)生產(chǎn)質(zhì)量管理體系建設(shè)作業(yè)指導(dǎo)書
- 房屋買賣合同委托書
- 合伙人合作協(xié)議合同
- 企業(yè)危機管理作業(yè)指導(dǎo)書
- 第三方代付款協(xié)議書
- 三農(nóng)村環(huán)境保護與管理方案
- 建筑垃圾買賣合同
- 電網(wǎng)工程設(shè)備材料信息參考價(2024年第四季度)
- 2025年江蘇農(nóng)牧科技職業(yè)學(xué)院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 2025江蘇連云港市贛榆城市建設(shè)發(fā)展集團限公司招聘工作人員15人高頻重點提升(共500題)附帶答案詳解
- 2025年全年日歷-含農(nóng)歷、國家法定假日-帶周數(shù)豎版
- 《東北大學(xué)宣傳》課件
- 社會主義從空想到科學(xué)的發(fā)展
- 《軌道交通工程盾構(gòu)施工技術(shù)》 課件 項目2 盾構(gòu)構(gòu)造認知
- 《傳統(tǒng)發(fā)酵饅頭酸菜》課件
- 《港珠澳大橋演講》課件
- 《有機化學(xué)》課件-第十章 羧酸及其衍生物
- 人教版道德與法治五年級下冊《第一單元 我們一家人》大單元整體教學(xué)設(shè)計2022課標(biāo)
評論
0/150
提交評論