資源簡介
一個(gè)簡單的Java打字練習(xí)代碼,3個(gè).Java文件,自己寫的東西

代碼片段和文件信息
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?Threadframe?extends?Jframe?implements?ActionListener?{
???JTextField?showWordinputTextshowScore;????????????????????????????//申明3個(gè)文本框?qū)ο螅ㄒ茫?br/>???JButton?button;?????????????????????????????????????????????????????//申明1個(gè)按鈕對(duì)象(引用)
???
???【代碼2】????????????????????????????????????????????????????//用WordThread聲明一個(gè)giveWord線程對(duì)象(引用)?
???int?score=0;
???Threadframe()?{
??????showWord?=?new?JTextField(6);?????????????????????????????//創(chuàng)建文本框?qū)ο髎howWord(引用)--輸出給出的漢字
??????showWord.setFont(new?Font(““Font.BOLD72));??????????????//設(shè)置文本框showWord的字體
??????showWord.setHorizontalAlignment(JTextField.CENTER?);??????//設(shè)置文本框showWord的對(duì)齊方式
??????【代碼3】?????????????????????????????????????????????????//用WordThread構(gòu)建一個(gè)giveWord線程對(duì)象
??????giveWord.setJTextField(showWord);?????????????????????????//設(shè)置showWord對(duì)象不可編輯
??????giveWord.setSleepLength(10000);???????????????????????????//設(shè)置giveWord對(duì)象sleepLength變量值為10000
??????button=new?JButton(“開始“);???????????????????????????????//創(chuàng)建按鈕對(duì)象button(引用)
??????inputText?=?new?JTextField(10);???????????????????????????//創(chuàng)建文本框?qū)ο骾nputText(引用)--輸入漢字
??????showScore?=?new?JTextField(5);????????????????????????????//創(chuàng)建文本框?qū)ο髎howScore(引用)--輸出分?jǐn)?shù)
??????showScore.setEditable(false);?????????????????????????????//設(shè)置showScore不可編輯(不可輸入)
??????button.addActionListener(this);???????????????????????????//設(shè)置button的事件監(jiān)聽器為當(dāng)前對(duì)象(窗體)
??????inputText.addActionListener(this);????????????????????????//設(shè)置inputText的事件監(jiān)聽器為當(dāng)前對(duì)象(窗體)
??????add(buttonBorderLayout.NORTH);???????????????????????????//將button加到框架(邊界布局-在上部)
??????add(showWordBorderLayout.CENTER);????????????????????????//將showWord加到框架(邊界布局-在中部)
??????JPanel?southP=new?JPanel();???????????????????????????????//構(gòu)建一個(gè)JPanel對(duì)象(面板--容器)
??????southP.add(new?JLabel(“輸入漢字(回車):“));??????????????//創(chuàng)建一個(gè)標(biāo)簽并加入到southP面板中
??????southP.add(inputText);????????????????????????????????????//將文本框?qū)ο骾nputText加入southP面板中
??????southP.add(showScore);????????????????????????????????????//將文本框?qū)ο髎howScore加入southP面板中
??????add(southPBorderLayout.SOUTH);???????????????????????????//將southP面板加到框架(邊界布局-在下部)
??????setBounds(100100350180);???????????????????????????????//設(shè)置框架位置和大小
??????setVisible(true);?????????????????????????????????????????//設(shè)置框架顯示出來
??????setLocationRelativeTo(null);??????????????????????????????//設(shè)置窗體的位置--居中
??????setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
???}?
???public?void?actionPerformed(ActionEvent?e)?{?????????????????//單擊按鈕或在文本框中回車會(huì)產(chǎn)生ActionEvent
??????if(e.getSource()==button)?{
?????????if(!(giveWord.isAlive())){?????????????????????????????//判斷線程giveWord的run()方法是否還正在運(yùn)行????
?????????????【代碼4】??????????????????????????????????????????//用WordThread構(gòu)建一個(gè)giveWord線程對(duì)象
?????????????giveWord.setJTextField(showWord);??????????????????//設(shè)置showWord對(duì)象不可編輯
?????????????giveWord.setSleepLength(10000);????????????????????//設(shè)置giveWord對(duì)象sleepLength變量值為10000
?????????}
?????????try?{
?????????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????3894??2017-06-05?21:43??漢字打字練習(xí)\Threadfr
?????文件?????????283??2017-06-05?20:45??漢字打字練習(xí)\ThreadWordMainClass.java
?????文件????????1169??2017-06-05?21:32??漢字打字練習(xí)\WordThread.java
評(píng)論
共有 條評(píng)論