该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public class ThreadWordMainClass{
public static void main(String args[]){
new ThreadFrame().setTitle("汉字打字练习")
;
}
}
class WordThread extends Thread{
char word;
int startPosition=19968;//Unicode表的19968位置至32320上的汉字
int endPosition=32320;
JTextField showWord;
int sleepLength=6000;
public void setJTextField(JTextField t){
showWord = t;
showWord.setEditable(false);//编辑展示所打文字,出现错误
}
public void setSleepLength(int n){
sleepLength = n;
}
public void run(){
int k=startPosition;
while(true){
word=(char)k;
showWord.setText(""+word);
try{sleep(sleepLength);//调用sleep方法使得线程中断sleepLength毫秒
}
catch(InterruptedException e){}
k++;
if (k>=endPosition)
k=startPosition;
}
}
}
class ThreadFrame extends JFrame implements ActionListener{
JTextField showWord;
JButton button;
JTextField inputText,showScore;
WordThread giveWord;//用WordThread声明一个giveWord线程对象
int score=0;
ThreadFrame(){
showWord = new JTextField(6);
showWord.setFont(new Font("",Font.BOLD,72));
showWord.setHorizontalAlignment(JTextField.CENTER);
giveWord=new WordThread();//创建giveWord线程
giveWord.setJTextField(showWord);
giveWord.setSleepLength(5000);
button=new JButton("开始");
inputText=new JTextField(10);
showScore=new JTextField(5);
showScore.setEditable(false);//展示分数,判断错误
button.addActionListener(this);
inputText.addActionListener(this);
add(button,BorderLayout.NORTH);
add(showWord,BorderLayout.CENTER);
JPanel southP=new JPanel();
southP.add(new JLabel("输入汉字(回车):"));
southP.add(inputText);
southP.add(showScore);
add(southP,BorderLayout.SOUTH);
setBounds(100,100,350,180);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
if(!(giveWord.isAlive())){//判断所给单词正误
giveWord=new WordThread();//创建giveWord
giveWord.setJTextField(showWord);
giveWord.setSleepLength(5000);
}
try{
giveWord.start();//giveWord调用方法start()
}
catch(Exception exe){}
}
else if(e.getSource()==inputText){
if(inputText.getText().equals(showWord.getText()))
score++;
showScore.setText("得分:"+score);
inputText.setText(null);
}
}