Java实验十,打字小游戏

在这里插入图片描述

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
import java.util.Random;

public class TypingGame extends JFrame {
	JPanel contentPane;
	JTextField inputField;
	int clickCount, rightCount, randomInt;
	JLabel resultLabel, inputLabel, showLabel, showResult, showChar;
	/**contentPane:容器
	 * inputField: 输入字母文本框
	 * clickCount: 点击字母总数
	 * rightCount: 输入字母正确数
	 * randomInt:  随机数字,用于随机生成字母
	 * resultLabel:“正确率:”
	 * inputLabel: “请输入:”
	 * showLabel:  “显示字母:”
	 * showResult: 以百分数形式显示正确率
	 * showChar:   显示随机生成的字母*/
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TypingGame frame = new TypingGame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**作用:随机生成一个字母
	 * @return 随机生成的字母*/
	Character showLetter() {
		Random random = new Random();
		char c = 'A';
		randomInt = random.nextInt(26);
		return (char)(c + randomInt);
	}

	public TypingGame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		resultLabel = new JLabel("正确率:");
		resultLabel.setFont(new Font("宋体", Font.PLAIN, 22));
		resultLabel.setBounds(112, 96, 88, 18);
		contentPane.add(resultLabel);
		
		inputLabel = new JLabel("请输入:");
		inputLabel.setFont(new Font("宋体", Font.PLAIN, 22));
		inputLabel.setBounds(112, 142, 88, 18);
		contentPane.add(inputLabel);

		showLabel = new JLabel("显示字母:");
		showLabel.setFont(new Font("宋体", Font.PLAIN, 22));
		showLabel.setBounds(90, 50, 122, 18);
		contentPane.add(showLabel);
		showResult = new JLabel(" ");
		showResult.setForeground(Color.RED);//设置字体为红色
		showResult.setFont(new Font("宋体", Font.PLAIN, 24));
		showResult.setBounds(211, 92, 114, 29);
		contentPane.add(showResult);
		
		showChar = new JLabel(" ");
		showChar.setForeground(Color.BLUE);//设置字体为蓝色
		showChar.setFont(new Font("宋体", Font.PLAIN, 25));
		showChar.setBounds(210, 40, 85, 38);
		contentPane.add(showChar);
		
		inputField = new JTextField();
		inputField.setFont(new Font("宋体", Font.PLAIN, 22));
		inputField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				  /*按下空格键时开始游戏*/
				if (e.getKeyCode() == KeyEvent.VK_SPACE) {
					rightCount = 0;
					clickCount = 0;
					char c = showLetter();
					showChar.setText(" " + c);
				} /*记录文本框输入的字母数,并将之前输入的字母清空*/ 
				else if (e.getKeyCode() >= KeyEvent.VK_A && 
						   e.getKeyCode() <= KeyEvent.VK_Z) {
					clickCount++;
					inputField.setText(null);
				} /*按下回车键停止游戏,并以百分数形式显示正确率*/
				else if (e.getKeyCode() == KeyEvent.VK_ENTER ) {
					if (clickCount == 0) {
						showResult.setText("0.00%");
					} else {
						double d = 100.0 * rightCount / clickCount;
						String result = String.format("%.2f", d);
						showResult.setText(result + "%");
					}				
}
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				/**show:值为随机生成的字母
				 * input:值为输入的字母
				 */
				String show = showChar.getText().trim();
				String input = inputField.getText().trim();
				/*若输入字母正确,则正确数加1并产生下一个随机字母*/
				if (show.equals(input)) {
					rightCount++;
					char c = showLetter();
					showChar.setText(" " + c);
				}
			}
		});
		
		inputField.setBounds(209, 139, 86, 29);
		contentPane.add(inputField);
		inputField.setColumns(10);
	}
}

在这里插入图片描述

发布了50 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42250302/article/details/89531746
今日推荐