java简单实现一个小型的打字测试软件

这次的实验室要做一个用java实现一个打字测试软件,由于java还没有熟练,大多数监听的话只用到了actionlistener控件,而没有用到keylistener来实现对键盘的监听,所以一开始没什么头绪,后来翻了翻网上关于keylistener的实现和用法,于是恍然大悟,很快的实现了这个小小的应用。算是一次比较印象深刻的经历吧,写和调试足足用了三个多小时,累的我不行不行的了。

附上代码:

package test12;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class typeWriting extends JFrame implements ActionListener,KeyListener{
	JPanel p1=new JPanel();
	JPanel p2=new JPanel(new FlowLayout());
	JPanel p3=new JPanel();
	JTextArea textField1=new JTextArea(4,2);
	JTextArea textField2=new JTextArea();
	float right=0;
	JLabel showmessageJLabel=new JLabel("当前正确率:100%");
	JLabel l1=new JLabel();
	JLabel l2=new JLabel("难度:");
	JTextField l3=new JTextField(5);
	Font font=new Font("楷体", 1, 48);
	Font font1=new Font("黑体",0,14);
	JButton b1=new JButton();
	JButton b2=new JButton();
	int count=0;
	int i=0,len1=0;
	double r=0;
	char []y;
	
	
	
	public typeWriting () {
		super("打字练习软件");
		setSize(500,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		l1.setFont(font);
		l1.setText("打字练习软件");;
		p1.setLayout(new GridLayout(3,1));
		textField1.setEditable(false);
		textField2.addKeyListener(this);
		textField1.setFont(font1);
		textField2.setFont(font1);
		textField1.setLineWrap(true);
		textField2.setLineWrap(true);
		textField1.setWrapStyleWord(true);
		textField2.setWrapStyleWord(true);
		
		p1.add(textField1);
		p1.add(showmessageJLabel);
		p1.add(textField2);
		b1=new JButton("开始");
		b2=new JButton("退出");
		p2.add(b1);
		p2.add(b2);
		p2.add(l2);
		p2.add(l3);
		p3.add(l1);
		add(p1,BorderLayout.CENTER);
		add(p3,BorderLayout.NORTH);
		add(p2,BorderLayout.SOUTH);
		b1.addActionListener(this);
		b2.addActionListener(this);
		JOptionPane.showMessageDialog(null, "请选择要练习的内容:1.初级 2.中级 3.困难");
		this.setLocationRelativeTo(null);
		setVisible(true);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1){
			int key=0;
			key=Integer.valueOf(l3.getText());
			switch (key) {
			case 1:
				textField1.setText("abcdefghijklmnopqrstuvwxyz");
				break;
			case 2:
				textField1.setText("My father was a self-taught mandolin player. "
						+ "He was one of the best string instrument players in our town. "
						+ "He could not read music, but if he heard a tune a few times, "
						+ "he could play it. "
						+ "When he was younger, he was a member of a small country music b");
				break;
			case 3:
				textField1.setText("sdfmawieofnosandvoinqewoifnvoqwneiqwenfqnwefoinqwoien");
				break;
			default:
				break;
			}
			len1=textField1.getText().length();
		}
		if(e.getSource()==b2)
		{
			System.exit(0);
		}
		
	}
	
	
	public static void main(String [] argsS)
	{
		typeWriting t1=new typeWriting();
	}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if((int)e.getKeyChar()>31&&(int)e.getKeyChar()<127)
			{
				if(i>=textField1.getText().length())
				{
					JOptionPane.showMessageDialog(null, "超出个数");
				}
				
				double c=0;
				if(e.getKeyChar()==textField1.getText().charAt(i))
				{
					r++;i++;
				}
				else {
					i++;
				   }
				double f=i;
				c=(r/f)*100;
				showmessageJLabel.setText("当前正确率:"+c+"%");
				
			}
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
	

	

}
java简单实现一个小型的打字测试软件_第1张图片
java简单实现一个小型的打字测试软件_第2张图片

你可能感兴趣的:(java编程,java,布局,软件,打字,测试)