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

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