当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue 和 AWT-Windows。
AWT-EventQuecue 线程负责处理GUI事件,AWT-EventQuecue线程负责将窗体或组件绘制到桌面。JVM保证各个线程都有使用CPU资源的机会.
样列:
1 package tes;
2
3 import java.awt.Color;
4 import java.awt.FlowLayout;
5 import java.awt.Font;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JFrame;
10 import javax.swing.JLabel;
11 import javax.swing.JTextField;
12
13 /*
14 * 模拟一个打字游戏
15 * */
16 public class Example12_11
17 {
18 public static void main(String args [])
19 {
20 Wndow wnd= new Wndow();
21 wnd.setTitle("打字游戏");
22 wnd.setSleepTime(3000);
23 }
24 }
25
26 class Wndow extends JFrame implements ActionListener ,Runnable
27 {
28 JTextField inputLetter;
29 JLabel showLetter,showScore;
30 int score;
31 Thread giveLetter; //生成字母
32 Wndow()
33 {
34 init();
35 setBounds(100, 100, 400, 240);
36 //setBackground(Color.green);
37 setVisible(true);
38 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39 }
40 void init()
41 {
42 score=0;
43 setLayout(new FlowLayout());
44 giveLetter = new Thread(this);
45 inputLetter = new JTextField(6);
46 showLetter = new JLabel(" ",JLabel.CENTER);
47 showLetter.setFont(new Font("Arial",Font.BOLD,22));
48 showScore =new JLabel("分数:");
49 add(new JLabel("显示字母:"));
50 add(showLetter);
51 add(new JLabel("输入字母按回车"));
52 add(inputLetter);
53 inputLetter.addActionListener(this);
54 add(showScore);
55 giveLetter.start();
56 }
57
58 @Override
59 public void run() {
60 // TODO Auto-generated method stub
61 // String reg="[a-zA-Z]+"; //正则表达式
62 int type[]={65,97};
63 while(true)
64 {
65 char cc=(char)(((int)(Math.random()*100))%26+type[(int)(Math.random()*1000)%2]);
66 //if(reg.matches(""+cc+""))
67 {
68 showLetter.setText(""+cc+" ");
69 validate(); //更改容器,所以得用上
70 try {
71 Thread.sleep(1000);
72 } catch (InterruptedException e) {
73 // TODO Auto-generated catch block
74 //e.printStackTrace();
75 }
76 }
77 }
78 }
79 @Override
80 public void actionPerformed(ActionEvent e) {
81 // TODO Auto-generated method stub
82 String get = inputLetter.getText().trim(); /*trim()方法的作用为删除多余的空格*/
83 String show =showLetter.getText().trim();
84 if(get.equals(show))
85 {
86 score++;
87 showScore.setText(""+score+" ");
88 validate();
89 }
90 inputLetter.setText(null);
91 giveLetter.interrupt(); //吵醒休眠的线程,以便加快出字母的速度
92 }
93 void setSleepTime(long aa)
94 {
95 try {
96 Thread.sleep(aa);
97 } catch (InterruptedException e) {
98 // TODO Auto-generated catch block
99 e.printStackTrace();
100 }
101 }
102 }
------->
代码:计时器
1 package tes;
2
3 import java.awt.FlowLayout;
4 import java.awt.Font;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.IOException;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11 import javax.swing.JButton;
12 import javax.swing.JFrame;
13 import javax.swing.JTextArea;
14
15
16 public class Example12_12 {
17 public static void main(String args [])
18 {
19 wndowThread wnd = new wndowThread();
20 wnd.setTitle("计时器");
21 }
22 }
23
24 class wndowThread extends JFrame implements ActionListener,Runnable
25 {
26 Thread givetime;
27 JTextArea showtime;
28 JButton start,end;
29 Date showdate;
30 boolean flag=false,judge=true;
31 SimpleDateFormat myformat = new SimpleDateFormat("hh:mm:ss");
32 wndowThread()
33 {
34
35 setSize(400, 240); /*大小*/
36 setLocation(100, 100); /*窗口位置*/
37 init();
38 setVisible(true); /*可视*/
39 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40 }
41 void init()
42 {
43 givetime = new Thread(this);
44 showtime = new JTextArea(10,30);
45 /*showtime.setLocation(10, 8);*/
46 start =new JButton("START");
47 start.setFont(new Font("Arial",Font.BOLD,22));
48 start.addActionListener(this);
49 end = new JButton("END");
50 end.addActionListener(this);
51 end.setFont(new Font("Arial",Font.BOLD,22));
52 setLayout(new FlowLayout());
53 add(start);
54 add(end);
55 add(showtime);
56 givetime.start();
57 }
58 @Override
59 public void actionPerformed(ActionEvent e) {
60 // TODO Auto-generated method stub
61 if(e.getSource()==start)
62 {
63 //先判断线程是否结束
64 if(!(givetime.isAlive()))
65 {
66 givetime =new Thread(this);
67 flag=false;
68 judge=true;
69 }
70 try {
71 givetime.start();
72 } catch (Exception e1) {
73 // TODO Auto-generated catch block
74 judge=false;
75 showtime.setText("线程没有结束run方法之前,不要再调用start方法.");
76 validate();
77 }
78 }
79 else if(e.getSource()==end)
80 {
81 flag=true;
82 if(judge==false)
83 {
84 showtime.setText("");
85 validate();
86 }
87 }
88 }
89
90 @Override
91 public void run() {
92 // TODO Auto-generated method stub
93 while(true) {
94 if(flag==true) return ;
95 showdate =new Date();
96 showtime.append("\n\t\t"+myformat.format(showdate));
97 try {
98 Thread.sleep(1000);
99 } catch (InterruptedException e) {
100 // TODO Auto-generated catch block
101 e.printStackTrace();
102 }
103 }
104 }
105
106 }
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有