import java.awt.*;
import java.awt.event.*; public class E10_1{ public static void main(String[] args){ //TODO Auto-generated method stub new MainFrame(); } }
class WordThread extends Thread{ //通过继承Thread类创建线程 char word; int k=19968; Label show; WordThread(Label show){ this.show=show; } public void run(){ //重载run函数 k=19968; while(true){ word=(char)k; show.setText(\ try{sleep(6000);} catch(InterruptedException e){} k++; if(k>=29968)k=19968; } } }
class MainFrame extends Frame implements ActionListener{ Label wordLabel; Button button; TextField inputText,scoreText; WordThread giveWord; int score=0; MainFrame(){ wordLabel=new Label(\ wordLabel.setFont(new Font(\ button=new Button(\开始\
}
inputText=new TextField(3); scoreText=new TextField(5); scoreText.setEditable(false); giveWord=new WordThread(wordLabel); button.addActionListener(this); inputText.addActionListener(this); add(button,BorderLayout.NORTH); add(wordLabel,BorderLayout.CENTER); Panel southPanel=new Panel(); southPanel.add(new Label(\输入标签所显示的汉字,然后回车:\ southPanel.add(inputText); southPanel.add(scoreText); add(southPanel,BorderLayout.SOUTH); setBounds(100,100,350,180); setVisible(true); validate(); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }
public void actionPerformed(ActionEvent e){ if (e.getSource()==button){ if(!(giveWord.isAlive())) { giveWord=new WordThread(wordLabel); } try{ giveWord.start();} catch(Exception exe){} } else if(e.getSource()==inputText){ if(inputText.getText().equals(wordLabel.getText())){ score++; } scoreText.setText(\得分\ inputText.setText(null); } }