打字母游戏2

来源:互联网 发布:自动下棋软件 编辑:程序博客网 时间:2024/06/11 03:56

比这一篇打字母游戏又多了一点东西、在界面上多了一个软键盘,当出现字母时,软键盘上对应字母会变绿。

当你在键盘上按下字母时,软键盘会显示你按下的那个字母

package Example12_10;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 ActionListener,KeyListener,Runnable{private static final long serialVersionUID = 966169356622696479L;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.addActionListener(this);inputLetter.addKeyListener(this);setBounds(100,100,515,280);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){}}}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();}else{if(score>0)score--;showScore.setText("得分:"+score);inputLetter.setText(null);validate();}}@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());if(key.length()==1){char ch=key.charAt(0);if(ch>='A'&&ch<='Z'){Object x=map.get(ch);button[(Integer) x].setEnabled(false);}}}@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
原创粉丝点击