zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Android 主题theme说明 摘记
    Android开发 去掉标题栏方法 摘记
    安卓项目五子棋代码详解(二)
    关于 ake sure class name exists, is public, and has an empty constructor that is public
    百度地图3.0实现图文并茂的覆盖物
    android onSaveInstanceState()及其配对方法。
    关于集成科大讯飞语音识别的 一个问题总结
    android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
    C# 解析 json Newtonsoft果然强大,代码写的真好
    c#数据类型 与sql的对应关系 以及 取值范围
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3603850.html
Copyright © 2011-2022 走看看