java的上机步骤,菜鸟跪求解释程序步骤,明天上机要用,帮我的人都是好人!!!...

本文介绍了如何使用Java实现一个汉字打字练习应用,通过Thread和ThreadFrame类展示了线程的使用,以及如何设置线程睡眠时间和文字展示。实现实时计分功能,适合初学者理解并发编程基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值