Source code of Java-based typing exercises and java-based typing exercises

Source: Internet
Author: User

Source code of Java-based typing exercises and java-based typing exercises

The Code is as follows:

Import java. applet. applet; import java. applet. audioClip; import java. awt. dimension; import java. awt. font; import java. awt. toolkit; import javax. swing. JFrame; import javax. swing. JPanel; import java. awt. rectangle; import javax. swing. borderFactory; import javax. swing. JButton; import javax. swing. JOptionPane; import javax. swing. JSlider; import javax. swing. JLabel; import javax. swing. UIManager; import javax. swing. event. changeEvent; import javax. swing. event. changeListener; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. keyAdapter; import java. awt. event. keyEvent; import java. io. file; import java. util. vector; public class Main extends JFrame implements Runnable {private static final long serialVersionUID =-inline; JPanel contentPane; JPanel jPanel1 = new JPanel (); JButton jButton1 = new JButton (); JSlider jSlider1 = new JSlider (); JLabel jLabel1 = new JLabel (); JButton jButton2 = new JButton (); JLabel jLabel2 = new JLabel (); int count = 1, rapidity = 5; int zhengque = 0, cuowu = 0; int rush [] = {10, 20, 30}; int rush_count = 0; char list [] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', 'U', 'V', 'w', 'x', 'y', 'z', '1', '2 ', '3', '4', '5', '6', '7', '8', '9'}; Vector number = new Vector (); string paiduan = "true"; AudioClip Musci_anjian, Music_shibai, plaintext; public Main () {try {setdefaclocloseoperation (EXIT_ON_CLOSE); // specify the audio file upload Musci_anjian = Applet. newAudioClip (new File ("sounds // anjian.wav "). toURL (); Music_shibai = Applet. newAudioClip (new File ("sounds // shibai.wav "). toURL (); Music_chenggong = Applet. newAudioClip (new File ("sounds // chenggong.wav "). toURL (); // ----------------------------------------- jbInit ();} catch (Exception exception) {exception. printStackTrace () ;}}/*** initialize ** @ throws Exception */private void jbInit () throws Exception {contentPane = (JPanel) getContentPane (); contentPane. setLayout (null); setSize (new Dimension (588,530); setTitle ("Frame Title"); jPanel1.setBorder (BorderFactory. createEtchedBorder (); jPanel1.setBounds (new Rectangle (4, 4,573,419); jPanel1.setLayout (null); jButton1.setBounds (new Rectangle (277,442, 89, 31 )); jButton1.setText (""); jButton1.addActionListener (new frameworkjbutton?actionadapter (this); merge (new Rectangle (83,448,164, 21); merge (100); jSlider1.setMinimum (0 ); jSlider1.setValue (95); jLabel1.setText ("Speed"); jLabel1.setBounds (new Rectangle (35,451, 39, 18); jButton2.setBounds (new Rectangle (408,442, 89, 31 )); jButton2.setText ("end"); jButton2.addActionListener (new listener (this); jLabel2.setText ("First off: 10"); jLabel2.setBounds (new Rectangle (414,473,171, 21); contentPane. add (jPanel1); contentPane. add (jButton2); contentPane. add (jButton1); contentPane. add (jSlider1); contentPane. add (jLabel1); contentPane. add (jLabel2); this. addKeyListener (new MyListener (); jButton1.addKeyListener (new MyListener (); jSlider1.addKeyListener (new MyListener (); listener (new ChangeListener () {public void stateChanged (ChangeEvent e) {rapidity = 100-jSlider1.getValue () ;}}) ;}/ *** main thread */public void run () {number. clear (); zhengque = 0; cuowu = 0; paiduan = "true"; while (count <= rush [rush_count]) {try {Thread t = new Thread (new Tthread (); t. start (); count + = 1; Thread. sleep (50 + (int) (Math. random () * 500); // The pause time of the production group. // the fastest speed is 0.05 seconds. slowest 0.5 seconds} catch (InterruptedException e) {e. printStackTrace () ;}while (true) {// wait until the last character disappears if (number. size () = 0) {break;} if (zhengque = 0) {// for later division .. if all are correct or wrong, an error will occur. so .. zhengque = 1;} if (paiduan. equals ("true") {// determine whether it is a natural termination if (cuowu <= 2) {// The error cannot exceed two JOptionPane passes. showMessageDialog (null, "Congratulations, you have passed"); rush_count + = 1; // automatically add 1 off if (rush_count <rush. length) {if (rapidity> 10) {// when the speed is greater than 10, increase acceleration in-5. fear that the speed is too fast rapidity-= 5; // The speed is automatically reduced by 10 ms jSlider1.setValue (rapidity); // select the location} Thread t = new Thread (this); t. start ();} else {JOptionPane. showMessageDialog (null, "B... you have passed the customs .. "); rush_count = 0; count = 0 ;}} else {JOptionPane. showMessageDialog (null, "Please renew"); rush_count = 0; count = 0 ;}} else {rush_count = 0; count = 0 ;}} public void jbutton?action=med (ActionEvent e) {Thread t = new Thread (this); t. start ();} public void jbutton2_actionreceivmed (ActionEvent e) {count = rush [rush_count] + 1; paiduan = "flase ";} /*** move-down thread */class Tthread implements Runnable {public void run () {boolean fo = true; int Y = 0, X = 0; JLabel show = new JLabel (); show. setFont (new java. awt. font ("", Font. PLAIN, 33); jPanel1.add (show); X = 10 + (int) (Math. random () * 400); String parameter = list [(int) (Math. random () * list. length)] + ""; Bean bean = new Bean (); bean. setParameter (parameter); bean. setShow (show); number. add (bean); show. setText (parameter); while (fo) {// --------------------- move the character down -------------------- show. setBounds (new Rectangle (X, Y + = 2, 33, 33); try {Thread. sleep (rapidity);} catch (InterruptedException e) {e. printStackTrace ();} if (Y> = 419) {fo = false; for (int I = number. size ()-1; I> = 0; I --) {Bean bn = (Bean) number. get (I); if (parameter. equalsIgnoreCase (bn. getParameter () {cuowu + = 1; jLabel2.setText ("correct:" + zhengque + ", error:" + cuowu + ""); number. removeElementAt (I); Music_shibai.play (); break ;}}}}/*** keyboard listener **/class MyListener extends KeyAdapter {public void keyPressed (KeyEvent e) {String uu = e. getKeyChar () + ""; for (int I = 0; I <number. size (); I ++) {Bean bean = (Bean) number. get (I); if (uu. equalsIgnoreCase (bean. getParameter () {zhengque + = 1; number. removeElementAt (I); bean. getShow (). setVisible (false); jLabel2.setText ("correct:" + zhengque + ", error:" + cuowu + ""); Music_chenggong.play (); break ;}} musci_anjian.play () ;}} public static void main (String [] args) {try {UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ();} catch (Exception exception) {exception. printStackTrace ();} Main frame = new Main (); Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize (); Dimension frameSize = frame. getSize (); if (frameSize. height> screenSize. height) {frameSize. height = screenSize. height;} if (frameSize. width> screenSize. width) {frameSize. width = screenSize. width;} frame. setTitle ("java edition typing exercise software"); frame. setLocation (screenSize. width-frameSize. width)/2, (screenSize. height-frameSize. height)/2); frame. setVisible (true) ;}} class frameworkjbutton2_actionadapter implements ActionListener {private Main adaptee; frameworkjbutton2_actionadapter (Main adaptee) {this. adaptee = adaptee;} public void actionreceivmed (ActionEvent e) {adaptee. jbutton2_actionreceivmed (e) ;}} class frame?jbutton=actionadapter implements ActionListener {private Main adaptee; frame?jbutton=actionadapter (Main adaptee) {this. adaptee = adaptee;} public void actionreceivmed (ActionEvent e) {adaptee. jbutton?action=med (e);}/*** character class */class Bean {String parameter = null; JLabel show = null; public JLabel getShow () {return show;} public void setShow (JLabel show) {this. show = show;} public String getParameter () {return parameter;} public void setParameter (String parameter) {this. parameter = parameter ;}}

  
<Ignore_js_op>

Http://java.662p.com/thread-3797-1-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.