打字母游戏3

来源:互联网 发布:提高数据分析能力 编辑:程序博客网 时间:2024/04/27 15:26

感觉每次输入一个字母再按回车似乎太累了  就优化了一下 

按下字母无需按回车就可以

package 打字母;import java.awt.*;import java.util.*;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 KeyListener,Runnable{JTextField inputLetter;Thread giveLetter;JLabel showLetter,showScore;Panel panel1,panel2;JButton button[];int sleepTime,score,temp;Map map;public void setSleepTime(int sleepTime){this.sleepTime=sleepTime;}WindowTyped(){map=new HashMap();giveLetter=new Thread(this);inputLetter =new JTextField(6);showLetter=new JLabel(" ",JLabel.CENTER);showScore=new JLabel("分数:0");showScore.setFont(new Font("宋体",Font.PLAIN,18));showScore.setForeground(Color.red);showLetter.setFont(new Font("Arial",Font.BOLD,22));showLetter.setForeground(Color.blue);panel1=new Panel();panel1.add(new JLabel("显示字母:"));panel1.add(showLetter);panel1.add(new JLabel("输入所显示的字母"));panel1.add(inputLetter);panel1.add(showScore);setWord();add(panel1,BorderLayout.NORTH);add(panel2,BorderLayout.CENTER);inputLetter.addKeyListener(this);setBounds(100,100,515,280);setResizable(false);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);giveLetter.start();}void setWord(){String word1="QWERTYUIOP";String word2="ASDFGHJKL";String word3="ZXCVBNM";char ch;int pos_x=0,j=0;panel2=new Panel();panel2.setLayout(null);button=new JButton[26];for(int i=0;i<26;i++){if(i<=9){ch=word1.charAt(j++);button[i]=new JButton(String.valueOf(ch));button[i].setBounds(pos_x,20,50,30);pos_x+=50;map.put(ch,i );}else if(i<=18){if(i==10){j=0;pos_x=25;}ch=word2.charAt(j++);button[i]=new JButton(String.valueOf(ch));button[i].setBounds(pos_x,70,50,30);pos_x+=50;map.put(ch,i );}else{if(i==19){j=0;pos_x=75;}ch=word3.charAt(j++);button[i]=new JButton(String.valueOf(ch));button[i].setBounds(pos_x,120,50,30);pos_x+=50;map.put(ch,i );}panel2.add(button[i]);}}public void run(){while(true){int x=(int)(Math.random()*26)+0;//随机数char ch=(char)((char)x+'a');button[temp].setForeground(null);temp=(Integer) map.get((char)(ch-32));showLetter.setText(""+ch+"");validate();try{button[temp].setForeground(Color.green);Thread.sleep(sleepTime);}catch(InterruptedException e){}}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubString key=KeyEvent.getKeyText(e.getKeyCode());String s=showLetter.getText().trim();if(key.length()==1){char ch=key.charAt(0);if(ch>='A'&&ch<='Z'){Object x=map.get(ch);button[(Integer) x].setEnabled(false);}if(s.charAt(0)==(char)(ch+32)){score++;showScore.setText("得分:"+score);inputLetter.setText(null);validate();giveLetter.interrupt();}else{if(score>0)score--;showScore.setText("得分:"+score);inputLetter.setText(null);validate();}}}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stubString key=KeyEvent.getKeyText(e.getKeyCode());if(key.length()==1){char ch=key.charAt(0);if(ch>='A'&&ch<='Z'){Object x=map.get(ch);button[(Integer) x].setEnabled(true);}}}}


1 0
原创粉丝点击