当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue 和 AWT-Windows。
AWT-EventQuecue 线程负责处理GUI事件,AWT-EventQuecue线程负责将窗体或组件绘制到桌面。JVM保证各个线程都有使用CPU资源的机会.
样列:
package tes; import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField; /*
* 模拟一个打字游戏
* */
public class Example12_11
{
public static void main(String args [])
{
Wndow wnd= new Wndow();
wnd.setTitle("打字游戏");
wnd.setSleepTime(3000);
}
} class Wndow extends JFrame implements ActionListener ,Runnable
{
JTextField inputLetter;
JLabel showLetter,showScore;
int score;
Thread giveLetter; //生成字母
Wndow()
{
init();
setBounds(100, 100, 400, 240);
//setBackground(Color.green);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
score=0;
setLayout(new FlowLayout());
giveLetter = new Thread(this);
inputLetter = new JTextField(6);
showLetter = new JLabel(" ",JLabel.CENTER);
showLetter.setFont(new Font("Arial",Font.BOLD,22));
showScore =new JLabel("分数:");
add(new JLabel("显示字母:"));
add(showLetter);
add(new JLabel("输入字母按回车"));
add(inputLetter);
inputLetter.addActionListener(this);
add(showScore);
giveLetter.start();
} @Override
public void run() {
// TODO Auto-generated method stub
// String reg="[a-zA-Z]+"; //正则表达式
int type[]={65,97};
while(true)
{
char cc=(char)(((int)(Math.random()*100))%26+type[(int)(Math.random()*1000)%2]);
//if(reg.matches(""+cc+""))
{
showLetter.setText(""+cc+" ");
validate(); //更改容器,所以得用上
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String get = inputLetter.getText().trim(); /*trim()方法的作用为删除多余的空格*/
String show =showLetter.getText().trim();
if(get.equals(show))
{
score++;
showScore.setText(""+score+" ");
validate();
}
inputLetter.setText(null);
giveLetter.interrupt(); //吵醒休眠的线程,以便加快出字母的速度
}
void setSleepTime(long aa)
{
try {
Thread.sleep(aa);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------->
代码:计时器
package tes; import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea; public class Example12_12 {
public static void main(String args [])
{
wndowThread wnd = new wndowThread();
wnd.setTitle("计时器");
}
} class wndowThread extends JFrame implements ActionListener,Runnable
{
Thread givetime;
JTextArea showtime;
JButton start,end;
Date showdate;
boolean flag=false,judge=true;
SimpleDateFormat myformat = new SimpleDateFormat("hh:mm:ss");
wndowThread()
{ setSize(400, 240); /*大小*/
setLocation(100, 100); /*窗口位置*/
init();
setVisible(true); /*可视*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
givetime = new Thread(this);
showtime = new JTextArea(10,30);
/*showtime.setLocation(10, 8);*/
start =new JButton("START");
start.setFont(new Font("Arial",Font.BOLD,22));
start.addActionListener(this);
end = new JButton("END");
end.addActionListener(this);
end.setFont(new Font("Arial",Font.BOLD,22));
setLayout(new FlowLayout());
add(start);
add(end);
add(showtime);
givetime.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==start)
{
//先判断线程是否结束
if(!(givetime.isAlive()))
{
givetime =new Thread(this);
flag=false;
judge=true;
}
try {
givetime.start();
} catch (Exception e1) {
// TODO Auto-generated catch block
judge=false;
showtime.setText("线程没有结束run方法之前,不要再调用start方法.");
validate();
}
}
else if(e.getSource()==end)
{
flag=true;
if(judge==false)
{
showtime.setText("");
validate();
}
}
} @Override
public void run() {
// TODO Auto-generated method stub
while(true) {
if(flag==true) return ;
showdate =new Date();
showtime.append("\n\t\t"+myformat.format(showdate));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }