JAVA多线程实现简单的点名系统

效果图如下:

 

CMain函数:

 1 package com.shubing.main;
 2 
 3 public class CMain
 4 {
 5     public static void main(String[] args) 
 6     {
 7         MainFrame mainFrame = new MainFrame();
 8         mainFrame.setVisible(true);
 9     }
10 }
View Code

MainFrame函数:

 1 package com.shubing.main;
 2 
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 import javax.swing.*;
 6 
 7 public class MainFrame extends JFrame implements ActionListener
 8 {
 9     public static boolean flag = false;
10     public JButton enter = new JButton("确定");
11     public JButton exit = new JButton("退出");
12     public JLabel text = new JLabel("点名");
13     public JLabel name = new JLabel("姓名");
14     public String[] nameList = {"王一","刘二","赵三","周四","马五","龙六","张七","贾八","钟九"};
15     public JLabel message = new JLabel("学生总数:"+nameList.length);
16     public EnterThread mainThread = null;
17     public MainFrame()
18     {
19         this.setTitle("点名系统");
20         this.setSize(230,150);
21         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22         this.setLocationRelativeTo(null);
23         this.setLayout(null);    //布局为空;
24         this.setFocusable(false);
25     
26         text.setBounds(20, 10, 100, 30);
27         name.setBounds(100, 10, 180, 30);
28         enter.setBounds(20, 50, 80, 20);
29         exit.setBounds(110, 50, 80, 20);
30         message.setBounds(120, 80, 100, 20);
31         
32         this.add(text);
33         this.add(name);
34         this.add(enter);
35         this.add(exit);
36         this.add(message);
37         
38         enter.setEnabled(true);
39         exit.setEnabled(false);
40         
41         Font font = new Font("华文行楷",Font.BOLD,22);
42         this.text.setFont(font);
43         this.name.setFont(font);
44         
45         enter.addActionListener(this);
46         exit.addActionListener(this);
47     }
48     
49     @Override
50     public void actionPerformed(ActionEvent e) 
51     {
52         if(e.getSource() == enter)
53         {
54             mainThread = new EnterThread(this);
55             mainThread.start();
56             enter.setEnabled(false);
57             exit.setEnabled(true);
58         }
59         if(e.getSource() == exit)
60         {
61             new ExitThread(this).start();
62             exit.setEnabled(false);
63         }
64         
65     }
66     
67 }
View Code

EnterThread函数:

 1 package com.shubing.main;
 2 
 3 public class EnterThread extends Thread
 4 {
 5     private MainFrame mainFrame = null;
 6     private boolean isStop = true;
 7     public int time = 50;
 8     
 9     public EnterThread(MainFrame mainFrame)
10     {
11         this.mainFrame = mainFrame;
12     }
13     public void stopThread()
14     {
15         this.isStop = false;
16     }
17     public void run()
18     {
19         try 
20         {
21             int index = 0;
22             while(isStop)
23             {
24                 if(index == this.mainFrame.nameList.length)
25                 {
26                     index = 0;
27                 }
28                 this.mainFrame.name.setText(mainFrame.nameList[index]);
29                 index ++;
30 
31                 Thread.sleep(time);
32             }
33         } 
34         catch (InterruptedException e) 
35         {
36             e.printStackTrace();
37         }
38         this.mainFrame.enter.setEnabled(true);
39     }
40 }
View Code

ExitThread函数:

 1 package com.shubing.main;
 2 
 3 public class ExitThread extends Thread
 4 {
 5     private MainFrame mainFrame = null;
 6 
 7     public ExitThread(MainFrame mainFrame)
 8     {
 9         this.mainFrame = mainFrame;
10     }
11 
12     public void run()
13     {
14         try 
15         {
16             for(int i=0;i<=400;i+=50)
17             {
18                 this.mainFrame.mainThread.time += i;
19                 sleep(1600);
20             }
21         } 
22         catch (InterruptedException e) 
23         {
24             e.printStackTrace();
25         }
26         mainFrame.mainThread.stopThread();
27     }
28 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值