打字母游戏
来源:互联网 发布:小牛在线网络借贷 编辑:程序博客网 时间:2024/04/27 15:42
利用线程控制字母出现的时间,训练用户寻找键盘上字母的快速能力。
随机出现26位小写字母的一个 输入后按回车即可。
如果输入正确分数+1,字母立刻改变。如果输入错误,分数-1,字母根据线程设置的sleep时间改变
package Example12_10;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Example12_10 {public static void main(String args[]){WindowTyped win=new WindowTyped();win.setTitle("打字母游戏");win.setSleepTime(3000);}}class WindowTyped extends JFrame implements ActionListener,Runnable{JTextField inputLetter;Thread giveLetter;JLabel showLetter,showScore;int sleepTime,score;public void setSleepTime(int sleepTime){this.sleepTime=sleepTime;}WindowTyped(){setLayout(new FlowLayout());giveLetter=new Thread(this);inputLetter =new JTextField(6);showLetter=new JLabel(" ",JLabel.CENTER);showScore=new JLabel("分数:");showScore.setFont(new Font("宋体",Font.PLAIN,18));showScore.setForeground(Color.red);showLetter.setFont(new Font("Arial",Font.BOLD,22));showLetter.setForeground(Color.blue);add(new JLabel("显示字母:"));add(showLetter);add(new JLabel("输入所显示的字母(回车)"));add(inputLetter);add(showScore);inputLetter.addActionListener(this);setBounds(100,100,400,280);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);giveLetter.start();}public void run(){while(true){int x=(int)(Math.random()*26)+0;//随机数showLetter.setText(""+(char)((char)x+'a')+"");validate();try{Thread.sleep(sleepTime);}catch(InterruptedException e){}}}public void actionPerformed(ActionEvent e){String s=showLetter.getText().trim();String letter=inputLetter.getText().trim();if(s.equals(letter)){score++;showScore.setText("得分"+score);inputLetter.setText(null);validate();giveLetter.interrupt();}}}
这是结果
1 0
- 打字母游戏
- c++打字母游戏
- 打字母游戏
- 打字母游戏
- 打字母游戏
- 打字母游戏2
- 打字母游戏3
- 打字母游戏+贴图技术
- 黑马学习---------------------android打字母游戏
- [JAVA]简单的打字母游戏
- Poedu_C语言_20160925_打字母游戏
- 一个打字母游戏 写的很乱
- Java图形界面实战案例——实现打字母游戏
- Po学校Mark老师教的打字母游戏
- C#编写一个打字母游戏,详细代码。。。。。
- 字母游戏
- 字母游戏
- 字母游戏
- 汉诺塔问题
- linux,unix,bsd命令收集
- 运行mahout 将学习集的序列化转化为向量报错Error: Java heap space(mapreduce运行内存调优)
- window官方 虚拟机使用系统,避免版权问题
- 求最长递增子序列长度并输出序列
- 打字母游戏
- 8.2.2.2 Speed of UPDATE Statements 加速UPDATE语句:
- 原子变量AtomicInteger
- 笔记
- String Programming Guide
- DRAM,SRAM,SDRAM区别
- wordpress操作数据库
- LeetCode 31: Next Permutation
- UIButton 上的图文上下排列