额,最后一个JAVA复习

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreadFrame extends JFrame implements ActionListener {
   JTextField showWord,inputText,showScore;                            //申明3个文本框对象(引用)
   JButton button;                                                     //申明1个按钮对象(引用)
  
   WordThread giveWord;                                                    //用WordThread声明一个giveWord线程对象(引用)
   int score=0;
   ThreadFrame() {
      showWord = new JTextField(6);                             //创建文本框对象showWord(引用)--输出给出的汉字
      showWord.setFont(new Font("",Font.BOLD,72));              //设置文本框showWord的字体
      showWord.setHorizontalAlignment(JTextField.CENTER );      //设置文本框showWord的对齐方式
      giveWord = new WordThread();                     //用WordThread构建一个giveWord线程对象
      giveWord.setJTextField(showWord);                         //设置showWord对象不可编辑
      giveWord.setSleepLength(10000);                           //设置giveWord对象sleepLength变量值为10000
      button=new JButton("开始");                               //创建按钮对象button(引用)
      inputText = new JTextField(10);                           //创建文本框对象inputText(引用)--输入汉字
      showScore = new JTextField(5);                            //创建文本框对象showScore(引用)--输出分数
      showScore.setEditable(false);                             //设置showScore不可编辑(不可输入)
      button.addActionListener(this);                           //设置button的事件监听器为当前对象(窗体)
      inputText.addActionListener(this);                        //设置inputText的事件监听器为当前对象(窗体)
      add(button,BorderLayout.NORTH);                           //将button加到框架(边界布局-在上部)
      add(showWord,BorderLayout.CENTER);                        //将showWord加到框架(边界布局-在中部)
      JPanel southP=new JPanel();                               //构建一个JPanel对象(面板--容器)
      southP.add(new JLabel("输入汉字(回车):"));              //创建一个标签并加入到southP面板中
      southP.add(inputText);                                    //将文本框对象inputText加入southP面板中
      southP.add(showScore);                                    //将文本框对象showScore加入southP面板中
      add(southP,BorderLayout.SOUTH);                           //将southP面板加到框架(边界布局-在下部)
      setBounds(100,100,350,180);                               //设置框架位置和大小
      setVisible(true);                                         //设置框架显示出来
      setLocationRelativeTo(null);                              //设置窗体的位置--居中
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void actionPerformed(ActionEvent e) {                 //单击按钮或在文本框中回车会产生ActionEvent
      if(e.getSource()==button) {
         if(!(giveWord.isAlive())){                             //判断线程giveWord的run()方法是否还正在运行   
             giveWord=new WordThread();                                          //用WordThread构建一个giveWord线程对象
             giveWord.setJTextField(showWord);                  //设置showWord对象不可编辑
             giveWord.setSleepLength(10000);                    //设置giveWord对象sleepLength变量值为10000
         }
         try {
             giveWord.start();                                  //giveWord调用方法start()--启动线程
         }
         catch(Exception exe){}
      }
      else if(e.getSource()==inputText) {                       //String类重写了Object类的方法equals()--参数是一个对象
          if(inputText.getText().equals(showWord.getText()))    //inputText.getText()获得文本框内容(String类型的对象)
              score++;
          showScore.setText("得分:"+score);
          inputText.setText(null);                              //清空文本框
      }
   }
}

 

 

 

 

 

 

public class ThreadWordMainClass {
  public static void main(String args[]) {           //程序的入口
      new ThreadFrame().setTitle("汉字打字练习");    //构造一个ThreadFrame对象(JFrame框架)
   }                                                 //并设置其标题为--汉字打字练习
}

posted @ 2017-11-21 22:44  宋建楠  阅读(390)  评论(0)    收藏  举报