java开发的游戏测试_用java编写。模拟一个简单的测试打字速度的游戏

请高手帮我看看我的程序哪里出现错误了,输出打字速度时出错importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.text.ParseExcepti...

请高手帮我看看我的程序哪里出现错误了,输出打字速度时出错

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.*;

public class TestTypeSpeed extends JFrame{

JTextArea ta1; //显示开始时间JTextArea ta2; //显示结束时间JTextArea ta3; //显示打字速度JTextField tf; //打字区

Calendar c1=Calendar.getInstance(),c2=Calendar.getInstance();

Date d;SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");double time;

public static void main(String[] args) {

new TestTypeSpeed();

}

public TestTypeSpeed() {

initCenter();initEast();initJFrame();

}

private void initJFrame() {

this.setBounds(200,200, 400, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

private void initCenter() {

JPanel p1 = new JPanel();

p1.setLayout(new BorderLayout());

JLabel l1 = new JLabel("测试打字速度");

l1.setBackground(Color.YELLOW);

p1.add(l1,BorderLayout.NORTH);

JPanel p3 = new JPanel();

p3.setLayout(new GridLayout(3,2));

JButton b1 = new JButton("开始");

p3.add(b1);

Monitor8 h = new Monitor8();

b1.addActionListener(h);

ta1= new JTextArea();

p3.add(ta1);

JButton b2 = new JButton("结束");

p3.add(b2);

b2.addActionListener(h);

ta2 = new JTextArea();

p3.add(ta2);

p1.add(p3,BorderLayout.CENTER);

JButton b3 = new JButton("您的打字速度为");

p3.add(b3);

b3.addActionListener(h);

ta3 = new JTextArea();

p3.add(ta3);

this.add(p1);

}

private void initEast(){

JPanel p2 = new JPanel();

p2.setLayout(new BorderLayout());

this.add(p2,BorderLayout.EAST);

JLabel ta3 = new JLabel("打字区");

p2.add(ta3,BorderLayout.NORTH);

tf = new JTextField(10);

p2.add(tf,BorderLayout.CENTER);

this.add(p2,BorderLayout.EAST);

}

private String getTheTime(Calendar c){

d = c.getTime();

String date = sdf.format(d);

return date;

}

private double getTimeInterval(){

double second = c2.get(Calendar.SECOND) - c1.get(Calendar.SECOND);

double minute = c2.get(Calendar.MINUTE) - c2.get(Calendar.MINUTE);

double hour = c2.get(Calendar.HOUR_OF_DAY) - c1.get(Calendar.HOUR_OF_DAY);

time = hour*3600+minute*60+second;

return time;

}

private class Monitor8 implements ActionListener{

public void actionPerformed(ActionEvent e) {

String comn = e.getActionCommand();

if(comn.equals("开始")){

Calendar c1 = Calendar.getInstance();

ta1.append(getTheTime(c1));

}

if(comn.equals("结束")){

Calendar c2 = Calendar.getInstance();

ta2.append(getTheTime(c2));

}

if(comn.equals("您的打字速度为")){

String tt = tf.getText();

int word = tt.length();

ta3.append(tt.length()/getTimeInterval()+"字每秒");

}

}

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值