前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)

Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)

作者头像
Gxjun
发布2018-03-26 10:39:51
1.7K0
发布2018-03-26 10:39:51
举报
文章被收录于专栏:mlml

     当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue 和 AWT-Windows。

AWT-EventQuecue 线程负责处理GUI事件,AWT-EventQuecue线程负责将窗体或组件绘制到桌面。JVM保证各个线程都有使用CPU资源的机会.

   样列:

代码语言:javascript
复制
  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     }

------->

代码:计时器

代码语言:javascript
复制
  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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档