Java 打字竞赛a-z


Java 打字竞赛a-z

ab53874428097acfec351e597fd4f8e7.png


import javax.swing.*;
import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test extends JFrame implements ActionListener,Runnable
{
    JTextField input;
    Thread get,showtimes;
    JLabel showchar,showscore;
    JLabel showTime;
    JLabel xianshi1,xianshi2;
    Data data1;
    int flagsecond=1;
    long secondStart,secondend;
    int score=0;
    public test()
    {
        input=new JTextField(6);
        get= new Thread(this);
        get.setName("get");
        showtimes=new Thread(this);
        showtimes.setName("time");
        showchar=new JLabel("",JLabel.CENTER);
        showchar.setFont(new Font("Arial",Font.BOLD,22));
        showscore=new JLabel("得分:"+score);
        showTime=new JLabel("");
        xianshi1=new JLabel("显示字母:");
        xianshi2=new JLabel("请输入显示的字母(回车)");
        setLayout(new FlowLayout());
        add(xianshi1);
        add(showchar);
        add(xianshi2);
        add(input);
        add(showscore);
        add(showTime);
        input.addActionListener(this);
        setBounds(100,100,400,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("LHL's Typing contest");
        setVisible(true);
        get.start();
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(flagsecond==1)
        {
            secondStart=System.currentTimeMillis();
            flagsecond=0;
        }
        String a=showchar.getText().trim();
        String b=input.getText().trim();
        if(a.equals(b))
        {
            if(a.equals("z"))
            {
                secondend=System.currentTimeMillis();
                long Timecha=secondend-secondStart;
                showTime.setText("完成!      时间:"+Timecha/1000+"."+Timecha%1000+"秒");
                invalidate();
            }
            score++;
            showscore.setText("得分:"+score);
            input.setText(null);
            invalidate();
            get.interrupt();
        }
        else
        {
            showscore.setText("得分:"+score);
            input.setText(null);
            invalidate();
        }
        if(!a.equals("z"))
        {
            showTime.setText("    时间:"+(System.currentTimeMillis()-secondStart)/1000+"."+(System.currentTimeMillis()-secondStart)%1000+"秒");
            invalidate();
        }
    }

    @Override
    public void run()
    {
            char c='a';
            while(true)
            {
                showchar.setText(""+c+"");
                validate();
                if(c>='z')
                {
                    break;
                }
                c=(char)(c+1);
                try
                {
                    Thread.sleep(20000000);
                } catch (InterruptedException e)
                {

                }
            }
    }
    public static void main(String []args)
    {
        test t1=new test();
    }
}

文章作者: LHL
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 LHL !
评论
  目录