900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java开发的游戏测试_用java编写。模拟一个简单的测试打字速度的游戏

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

时间:2021-11-25 16:37:40

相关推荐

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()+"字每秒");

}

}

}

}

展开

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。