java的综合实验小游戏_Java实验十,打字小游戏

9872e99a4c235c062cb0c25f0824e6b8.png

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

}

}

41c784f4d82690aaf15a4a34b888c2ee.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值