JAVA资料1.1

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.awt.*;
import java.awt.event.*;

public class ThreadFrame extends Frame implements ActionListener {
Label wordLabel;
Button button;
TextField inputText, scoreText;
WordThread giveWord;// 用WordThread声明一个giveWord对象
int score = 0;

ThreadFrame() {
wordLabel = new Label(" ", Label.CENTER);
wordLabel.setFont(new Font("", Font.BOLD, 72));
button = new Button("开始");
inputText = new TextField(3);
scoreText = new TextField(5);
scoreText.setEditable(false);
giveWord=new WordThread(wordLabel);// 创建giveWord,将wordLabel传递给WordThread构造方法的参数
button.addActionListener(this);
inputText.addActionListener(this);
add(button, BorderLayout.NORTH);
add(wordLabel, BorderLayout.CENTER);
Panel southP = new Panel();
southP.add(new Label("输入标签所显示的汉字后回车:"));
southP.add(inputText);
southP.add(scoreText);
add(southP, 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调用方法isAlive()
{
giveWord = new WordThread(wordLabel);
}
try {
giveWord.start();// giveWord调用方法start()
} catch (Exception exe) {
}
} else if (e.getSource() == inputText) {
if (inputText.getText().equals(wordLabel.getText())) {
score++;
}
scoreText.setText("得分:" + score);
inputText.setText(null);
}
}
}

相关文档
最新文档