首页 > 代码库 > Java之线程———GUI线程

Java之线程———GUI线程

     当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     }
View Code

------->