java课程项目之幸运观众手机号码抽取器

一、      大型实验的内容

开发一个简单的幸运观众手机号码抽取程序,要求在理解Java多线程原理基础上,设定简单的抽取人数、获奖等级等参数后,能随机抽取存在文本文件中(每行放一个手机号和归属地)的若干个观众手机号码,显示时隐藏最后两位号码,并同时显示该手机号码的所属地。

 

二,、 系统功能具体描述为:

1、 设置获奖等级和抽奖人数

本程序默认为三等奖5人。

设置时会清空JexstField里的内容,并且会暂停抽取程序的运行,当设置人数的时候,中间可编辑的文本框也会改变。

2.开始按钮

点击时在文本框里不停的变换显示从information.txt里读取的手机号码

3、结束按钮

文本框内停止变换,显示结束时所显示的手机号码及地址

实现代码如下:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Luckymode extends JFrame implements Runnable {

    JLabel jnum,jtype; //文本”设置获奖人数:“
    JComboBox phonenum, prize; 
    JButton start, end;
    int number = 4;
    boolean isrun = false;
    int i;
    String textname="E:/information.txt";
    JTextField text[] = new JTextField[10];
    Vector<String> v=new Vector<String>();
    Luckymode(String s) {
        super(s);
        initComponents();
    }
    public void initComponents() {   //界面初始化及监听事件
        Container jtogether = getContentPane();
        JPanel jup = new JPanel();
        jup.setLayout(new FlowLayout());
        jnum = new JLabel("人数:");
        jtype=new JLabel("奖项:");
        String[] str = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
        String[] strtype={"一等奖","二等奖","三等奖"};
        prize = new JComboBox(strtype);
        prize.setSelectedIndex(2);
        prize.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                isrun = false;
                start.setEnabled(true);
                end.setEnabled(false);
                for (i = 0; i < 10; i++) {
                    text[i].setText("");
                }
            }
        });
        phonenum = new JComboBox(str);
        phonenum.setSelectedIndex(4);
        phonenum.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == phonenum) {
                    isrun = false;
                    start.setEnabled(true);
                    end.setEnabled(false);
                    number = phonenum.getSelectedIndex();
                    for (i = 0; i < 10; i++) {
                        text[i].setEditable(true);
                        text[i].setText("");
                    }
                    if (number != 10) {
                        for (i = number+1; i < 10; i++) {
                            
                            text[i].setEditable(false);
                        }
                    }
                }
            }

        });
        jup.add(jtype);
        jup.add(prize);
        jup.add(jnum);
        jup.add(phonenum);
        
        JPanel jtemp=new JPanel();
        jtemp.setLayout(new BorderLayout());
        JPanel j1=new JPanel();
        j1.add(new JLabel(" "));
        JPanel j2=new JPanel();
        j2.add(new JLabel(" "));
        JPanel jcenter = new JPanel();
        jcenter.setLayout(new GridLayout(10, 1, 20, 0));
        for (i = 0; i < 10; i++) {
            text[i] = new JTextField("");
            text[i].setHorizontalAlignment(JTextField.CENTER );
            jcenter.add(text[i]);
        }
        for (i = number+1; i < 10; i++) {
            text[i].setEditable(false);
        }
        jtemp.add("Center",jcenter);
        jtemp.add("West",j1);
        jtemp.add("East",j2);
        JPanel jdown = new JPanel();
        jdown.setLayout(new FlowLayout());
        start = new JButton("开始");
        end = new JButton("结束");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    isrun = true;
                    start.setEnabled(false);
                    end.setEnabled(true);
            }
        });
        end.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    isrun = false;
                    start.setEnabled(true);
                    end.setEnabled(false);
            }
        });
        end.setEnabled(false);
        jdown.add(start);
        jdown.add(end);
        jtogether.setLayout(new BorderLayout());
        jtogether.add("North", jup);
        jtogether.add("Center", jtemp);
        jtogether.add("South", jdown);
        textinfile();
        setVisible(true);
    }
    public String texthand(String a){//将从文件读入的数据处理
        String restring,s1,s2;
        s1=a.substring(11);
        s2=a.substring(0,9);
        restring=s2+"**"+s1;
        return restring;
     }
    public void textinfile()
    {
        FileReader f;    //将文件中手机号码信息读入vector
        try {
            f = new FileReader("E:/information.txt");
            BufferedReader br = new BufferedReader(f);
            String strline=br.readLine() ;        
            while(strline!=null){
                v.add(texthand(strline));
                strline=br.readLine();
            }
            br.close();
            f.close();
        }            
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void run() {      //线程运行
        int k=0;
        while (true) {
            if(isrun){
                int m=(int) (Math.random() * 100) + 50;
                try {
                    Thread.sleep(m);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                for (i = 0; i <=number; i++) { 
                    if(k>=v.size()){k=0;}
                    text[i].setText(v.get(k));  //在text[i]中随机显示手机号码
                    k++;            
                }    
            }
        }            
    }
    public static void main(String[] args) {
        Luckymode luck = new Luckymode("幸运手机号码抽取");
        luck.setSize(300, 450);
        luck.setLocation(800, 300);
        luck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Thread t1 = new Thread(luck);
            t1.start();
            Thread t2 = new Thread(luck);
            t2.start();
            Thread t3 = new Thread(luck);
            t3.start();
    }
}
        

效果演示:                                                            E盘的文件:

      

posted @ 2017-05-15 21:38  雨中枫玲  阅读(934)  评论(1编辑  收藏  举报