随笔-4  评论-4  文章-0  trackbacks-0

/*
*此小程序的基本流程是,在一个文本文件存放里面备选的数据,数据的格式是每行有姓名 和 手机号
*他们之间是用分隔符-来分隔的。然后在自定义一个方法,通过分隔符来区分姓名和手机号,然后将
*他们分别存储进两个不同的可变向量当中。
*   自定义一个线程,此线程是专门用来随机选取一个号码, 并且每隔50毫秒就将一个不同的号码显示在
*   号码框中,这样就实现了号码的快速滚动,实现了完全模拟电视当中抽奖的画面

*   为了实现选取出的一个号码不能再被选种,实现的方法是将他们从可变向量中清除出去

*   本程序的数据来源选用的是文本文件,原因有,第一,这是一个小程序,加上数据库显得不适宜。第二,本着
*   学习的态度,因为,如果用数据库来实现的话,很多功能都可以通过sql语句和 数据库的功能来实现。而此程序
*   所有的功能都需程序本身来承担完成。当然,对于有大量的数据来说,还是需要数据库的。像这样把数据一次性
*   读入内存中,大于大量数据是不行的!

*   此程序是在无界面设计的环境eclipse下设计完成的,界面难免显得僵硬....

*   在以后的版本中还将实现,用户可以自己设定 名额,奖项等....
*/
package youg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class ChooseAward extends JFrame
{
        /**
         *
         */
        //系统自动生成
        private static final long serialVersionUID = 7510608124693852355L;
        JButton b_start = new JButton("开始");
        JButton b_stop = new JButton("停止");
        JPanel p_north=new JPanel();
        JPanel p_center=new JPanel();
        JPanel p_south=new JPanel();

        //菜单栏
        JMenuBar menubar=new JMenuBar();
       
        //菜单项
        JMenu fileMenu=new JMenu("文件");
        JMenu setMenu=new JMenu("设置");
        JMenu aboutMenu=new JMenu("关于");
       
        /*
         * 菜单子项
         */
        private JMenuItem[] filem={new JMenuItem("选择文件"),new JMenuItem("另存为.."),
                        new JMenuItem("保存"),new JMenuItem("退出")
        };
        private JMenuItem[] filew={new JMenuItem("关于我们")};
       
        JTextField t_identNumber = new JTextField(); // 身份证号的文本域

        Vector v_identNumber = new Vector(); // 存放读取出来的号码

        Vector v_name = new Vector(); // 存放读取出来的人的姓名
       
        Vector z_identNumber = new Vector(); // 存放号码

        Vector z_name = new Vector(); // 存放姓名

        Vector v_printident = new Vector(); // 存放中奖人的身份证号码

        JLabel l_information = new JLabel();

        JLabel l_identNumber = new JLabel();

        JLabel l_sysinformation = new JLabel("系统信息:");

        JFileChooser filechooser = new JFileChooser(); // 文件选择器

        //选择随机数的线成
        public ChooseThread awardThread = null;
        public static int level=1;
        int chooseTime = 0; // 按下停止按钮的次数,也就是抽奖的次数
        public ChooseAward()
        {
                super("**抽奖系统");
                t_identNumber.setEditable(false);
                /*
                 * 1.4以后的JDK可以不用Container
                 */
                Container contentPane = getContentPane();
                //BorderLayout布局管理器,东,南,西,北
                contentPane.setLayout(new BorderLayout());
                /*
                 * 给几个按钮和子菜单添加监听器
                 */
                filem[0].addActionListener(new ActionListener()
                {
                        // 注册加载事件的监听器
                        public void actionPerformed(ActionEvent e)
                        {
                                b_loadident_ActionPerformed(e);
                                }
                        });
                b_start.addActionListener(new ActionListener()
                {         
                        // 注册开始事件的监听器
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                b_start_ActionPerformed(e);
                                        }
                                });
                b_stop.addActionListener(new ActionListener()
                {       // 注册停止事件的监听器
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                b_stop_ActionPerformed(e);
                                        }
                                });
                filem[2].addActionListener(new ActionListener()
                {      // 注册打印事件的监听器
                                        public void actionPerformed(ActionEvent e)
                                        {
                                                b_printaward_ActionPerformed(e);
                                        }
                                });
                addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e)
                        {
                                System.exit(0);
                        }
                });
                //加入相关信息
                filew[0].addActionListener(new ActionListener()
                        {
                                // 注册加载事件的监听器
                                public void actionPerformed(ActionEvent e)
                                {
                                        about_loadident_ActionPerformed(e);
                                        }
                                });
               
                //将菜单子项加入菜单中
                for(int i=0;i<filem.length;i++)
                {
                        fileMenu.add(filem[i]);
                        //加入分隔线,为了美观
                       
                                fileMenu.addSeparator();
                }
               
                for(int i=0;i<filew.length;i++)
                {
                        aboutMenu.add(filew[i]);
                        //加入分隔线,为了美观
                        //aboutMenu.addSeparator();
                }
                //将菜单条加入frame中
                menubar.add(fileMenu);
                menubar.add(setMenu);
                menubar.add(aboutMenu);
                setJMenuBar(menubar);
               
               
                /*将p_north设计为一个状态栏,显示一些操作过程中的信息
                 * 创建一个左对齐方式的流布局
                 */
                p_south.setLayout(new FlowLayout(FlowLayout.LEFT));
                l_information.setForeground(Color.blue);
                p_south.add(l_sysinformation);
                p_south.add(l_information);
                contentPane.add(p_south,BorderLayout.SOUTH);
                /*
                 * 设置的一些显示方式,字体,大小等,为了尽量美观一点
                 */
                Font xuehao=new Font("null",Font.PLAIN,30);
                l_identNumber.setFont(xuehao);
                //字体的居中对齐
                l_identNumber.setHorizontalAlignment(0);
                l_identNumber.setText("手机号");
               
               
                Font number=new Font("null",Font.BOLD,30);//粗体
                t_identNumber.setFont(number);
                //设置字体颜色
                t_identNumber.setForeground(Color.red);
                t_identNumber.setHorizontalAlignment(0);
               
               
               
                //给上面的jpanel设置布局
                p_north.setLayout(new GridLayout(2,1));
                p_north.add(l_identNumber);
                p_north.add(t_identNumber);
               
               
                contentPane.add(p_north,BorderLayout.NORTH);
                //给中间的jpanel 设置布局
                p_center.setLayout(new GridLayout(1,2,10,0));
               
                p_center.add(b_start);
                p_center.add(b_stop);
               
               
               
                contentPane.add(p_center,BorderLayout.CENTER);
               
        }

        /*
         * 加载按钮事件方法
         */
        public void b_loadident_ActionPerformed(ActionEvent e)
        {
                int k = 0;
                chooseTime = 0;
                //从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取
                BufferedReader reader = null;
                //此方法会返回一个int值
                int i = filechooser.showOpenDialog(this); // 显示打开文件对话框
                /*
                 * 确实所选择的是对话框上的确定按钮
                 */
                if (i == JFileChooser.APPROVE_OPTION)
                { // 点击对话框中打开选项
                        File f = filechooser.getSelectedFile(); // 得到所选择的文件
                        try
                        {
                                l_information.setText("数据加载中,请稍等...");
                                //读取字符流
                                reader = new BufferedReader(new FileReader(f));
                                //循环读取
                                while (true)
                                {
                                        //读取一个文本行
                                        String data = reader.readLine();
                                        System.out.println(data);//开发时候测试用
//                                        读完了数据
                                        if (data == null)
                                        {
                                            l_information.setText("数据加载完成!");
                                            break;
                                        }
                                       
                                        //通过“-”这个符号将 字符串分离为两部分,再存放到向量里面
                                        Vector v = this.apart(data, "-");
                                        //System.out.println(data);//开发时候测试用
                                        if (v == null)
                                        {
                                                l_information.setText("数据格式不正确,请重新加载!");
                                                return;
                                        }

                                        /*
                                         * 分别将分离开来的手机号码,和姓名分别放入两个可变向量当中
                                         * 这里应该使用泛型,由于本人还没有熟练掌握泛型,姑且放之
                                         *
                                         *
                                         * 捕获异常,看是否出现了格式错误,因为一般出现了格式错误
                                         * 这两条语句都会发生无法取得元素的错误
                                         */
                                        try
                                        {
                                        v_identNumber.add(k, v.elementAt(0));
                                        v_name.add(k, v.elementAt(1));
                                        k++;
                                        }
                                       
                                        catch(Exception e4)
                                        {
                                                System.out.println("格式中没有分隔符号出现的错误");
                                                //JOptionPane.showMessageDialog(this, "导入的数据格式错误!");
                                                l_information.setText("导入的数据格式错误!");
                                                break;
                                        }

                                }
                        } catch (Exception ex)
                        {
                                ex.printStackTrace(); // 输出出错信息
                        }
                }
        }
       
       
       
       
       
        /*
         * 加载关于按钮的事件
         */
        public void about_loadident_ActionPerformed(ActionEvent e)
        {
                int k = 0;
                chooseTime = 0;
                //从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取
                BufferedReader reader = null;
                //此方法会返回一个int值
                int i = filechooser.showOpenDialog(this); // 显示打开文件对话框
                /*
                 * 确实所选择的是对话框上的确定按钮
                 */
                if (i == JFileChooser.APPROVE_OPTION)
                { // 点击对话框中打开选项
                        File f = filechooser.getSelectedFile(); // 得到所选择的文件
                        try
                        {
                                l_information.setText("数据加载中,请稍等...");
                                //读取字符流
                                reader = new BufferedReader(new FileReader(f));
                                //循环读取
                                while (true)
                                {
                                        //读取一个文本行
                                        String data = reader.readLine();
                                        System.out.println(data);//开发时候测试用
//                                        读完了数据
                                        if (data == null)
                                        {
                                            l_information.setText("数据加载完成!");
                                            break;
                                        }
                                       
                                        //通过“*”这个符号将 字符串分离为两部分,再存放到向量里面
                                        Vector v = this.apart(data, "*");
                                        //System.out.println(data);//开发时候测试用
                                        if (v == null)
                                        {
                                                l_information.setText("数据格式不正确,请重新加载!");
                                                return;
                                        }

                                        /*
                                         * 分别将分离开来的手机号码,和姓名分别放入两个可变向量当中
                                         * 这里应该使用泛型,由于本人还没有熟练掌握泛型,姑且放之
                                         *
                                         *
                                         * 捕获异常,看是否出现了格式错误,因为一般出现了格式错误
                                         * 这两条语句都会发生无法取得元素的错误
                                         */
                                        try
                                        {
                                        z_identNumber.add(k, v.elementAt(0));
                                        z_name.add(k, v.elementAt(1));
                                        k++;
                                        }
                                       
                                        catch(Exception e4)
                                        {
                                                System.out.println("格式中没有分隔符号出现的错误");
                                                //JOptionPane.showMessageDialog(this, "导入的数据格式错误!");
                                                l_information.setText("导入的数据格式错误!");
                                                break;
                                        }

                                }
                        } catch (Exception ex)
                        {
                                ex.printStackTrace();   // 输出出错信息
                        }
                }
                String a=(String)z_name.lastElement();
                level=new Integer(a); //设定LEVEL的取值
                if(level<1||level>10)
                 level=5;
                z_identNumber.remove(z_identNumber.lastElement());  //将最后一个去掉
               
                System.out.println("level"+level+z_identNumber.lastElement());
        }

       
        /*
         * 启动按钮的事件
         */
        public void b_start_ActionPerformed(ActionEvent e)
        {
                //判断存储两个标记的向量中是否为空
                if (v_identNumber.size() <= 0 || v_name.size() <= 0)
                {
                        l_information.setText("数据没有加载,请加载数据!");
                }
                else
                {
                        if (chooseTime > 6)
                        {
                                l_information.setText("抽奖结束,若要再进行一次须重新启动程序!");
                                b_start.setEnabled(false);
                                b_stop.setEnabled(false);
                               
                        }
                        else//执行....
                        {
                                awardThread = new ChooseThread(this);
                                awardThread.changeflag_start();
                                l_information.setText("将产生:特(1名),一(1名),二(2名),三(3名)等奖");
                                l_identNumber.setText("选取中...");
                                b_start.setEnabled(false);
                                b_stop.setEnabled(true);
                        }
                }
        }

        /*
         * 暂停按钮的事件
         */
        public void b_stop_ActionPerformed(ActionEvent e)
        {
                //将跳转的数字置于停止状态
                awardThread.changeflag_stop();
                String awardmessage = "";
                chooseTime++;//第几次按停止按钮
                String str_name = "";
                String message="";
                if(chooseTime>6&&level>=10)
                {
                 t_identNumber.setText((String)z_identNumber.elementAt(0));
                }
                /*
                 * 根据第几次来按停止按钮来决定是产生几等奖
                 */
                switch (chooseTime)
                {
                case 1:
                case 2:       
                case 3://按前三次都是产生三等奖       
                        //寻找停止在号码框中的数字,是对应的的存放号码的向量中的第几个
                        for (int k = 0; k < v_identNumber.size(); k++)
                        {
                                //找到了对应的号码
                                if ((t_identNumber.getText()).equals(v_identNumber.elementAt(k)))
                                {
                                        //取出这个号码对应的名字
                                        str_name = (String) v_name.elementAt(k);
                                        //为防止下次抽的时候再抽到相同的号码,所以把它们从向量中移除掉
                                        v_identNumber.removeElementAt(k);
                                        v_name.removeElementAt(k);
                                        z_identNumber.removeElement(t_identNumber.getText());
                                        //z_name.removeElement(t_identNumber.getText());
                                        //z_name.removeElementAt(z_identNumber.indexOf(t_identNumber.getText()));
                                        break;//跳出循环
                                }
                        }
                        l_identNumber.setText("三等奖");
                        b_start.setText("继续");
                        //这是要输出到文本文件的信息
                        awardmessage = "三等奖  " + t_identNumber.getText() + str_name + "\r\n";
                        //将要打印的文本信息先存放到一个可变向量中
                        v_printident.addElement(awardmessage);//系统提示说要使用泛型,本人不会用,以后再研究
                        message = "第"+chooseTime+"位三等奖得主为:  "+str_name;
                        JOptionPane.showMessageDialog(this, message);
                        break;
                case 4:
                case 5:
                        for (int k = 0; k < v_identNumber.size(); k++)
                        {
                                if (t_identNumber.getText().equals(v_identNumber.elementAt(k)))
                                {
                                        str_name = (String) v_name.elementAt(k);
                                        v_identNumber.removeElementAt(k);
                                        v_name.removeElementAt(k);
                                        z_identNumber.removeElement(t_identNumber.getText());
                                        //z_name.removeElement(t_identNumber.getText());
                                        //z_name.removeElementAt(z_identNumber.indexOf(t_identNumber.getText()));
                                        break;
                                }
                        }
                        l_identNumber.setText("二等奖");
                        awardmessage = "二等奖  " + t_identNumber.getText() + str_name + "\r\n";
                        v_printident.addElement(awardmessage);
                        int serial=chooseTime-3;//第几位得主,排名序号
                        message = "第"+serial+"位二等奖得主为:  "+str_name;
                        JOptionPane.showMessageDialog(ChooseAward.this, message);
                        break;
                case 6:
                        for(int k=0;k<v_identNumber.size();k++)
                        {
                                if(t_identNumber.getText().equals(v_identNumber.elementAt(k)))
                                {
                                        str_name=(String) v_name.elementAt(k);
                                        v_identNumber.removeElementAt(k);
                                        v_name.removeElementAt(k);
                                        z_identNumber.removeElement(t_identNumber.getText());
                                        //z_name.removeElementAt(z_identNumber.indexOf(t_identNumber.getText()));
                                        break;
                                }
                        }
                        l_identNumber.setText("一等奖");
                        awardmessage = "一等奖  " + t_identNumber.getText() + str_name + "\r\n";
                        v_printident.addElement(awardmessage);
                        /*
                         * 第几位得主,排名序号,其实没必要这样写,因为本来就这一名
                         * 现在这样写的作用是为我以后改进这个程序(可以自主选择产生几名得奖主)
                         * 的时候做好准备
                         */
                        //serial=chooseTime-5;
                        //message = "第"+serial+"位二等奖得主为:  "+str_name;
                        message = "一等奖得主为:  "+str_name;
                        JOptionPane.showMessageDialog(ChooseAward.this, message);
                        break;
                case 7:
                        for (int k = 0; k < v_identNumber.size(); k++)
                        {
                                if (t_identNumber.getText().equals(v_identNumber.elementAt(k)))
                                {
                                        str_name = (String) v_name.elementAt(k);
                                        v_identNumber.removeElementAt(k);
                                        v_name.removeElementAt(k);
                                        z_identNumber.removeElement(t_identNumber.getText());
                                        //z_name.removeElement(t_identNumber.getText());
                                        //z_name.removeElementAt(z_identNumber.indexOf(t_identNumber.getText()));
                                        break;
                                }
                        }
                        l_identNumber.setText("特等奖");
                        awardmessage = "特等奖" + t_identNumber.getText() + str_name + "\r\n";
                        v_printident.addElement(awardmessage);
                        message = "特等奖得主为:  "+str_name;
                        JOptionPane.showMessageDialog(ChooseAward.this, message);        
                        l_information.setText("此次抽奖全部结束,保存抽奖结果!");
                        break;
                default:
                        JOptionPane.showMessageDialog(ChooseAward.this, "抽奖已经结束");
                        b_start.setText("开始");
                        awardThread.changeflag_stop();
                        break;
                }
                b_start.setEnabled(true);
                b_stop.setEnabled(false);
        }

        /*
         * 输出按钮的事件
         */
        public void b_printaward_ActionPerformed(ActionEvent e)
        {
                try
                {
                        FileOutputStream fs_out = new FileOutputStream("result.txt");
                        DataOutputStream out = new DataOutputStream(fs_out);
                        for (int i = 0; i < v_printident.size(); i++)
                        {
                                System.out.println(v_printident.elementAt(i));//测试用
                                out.writeUTF((String) v_printident.elementAt(i) + "\r\n");
                        }
                        out.close();
                        l_information.setText("文件输出成功!保存在当前目录下..");
                } catch (FileNotFoundException fe)
                {
                        System.err.println(fe);
                } catch (IOException ioe)
                {
                        System.err.println(ioe);
                }

        }

        /*
         * 定义一个方法,将号码和名字分离开来
         */
        //一个是源数据文件,一个是分隔符号
        public Vector apart(String src, String separator)
        {
                //使用了泛型,用可变向量来存放等到的标记
                Vector<String> v = new Vector<String>();
                /*
                 * string tokenizer 类允许应用程序将字符串分解为标记,祥见api文档
                 */
                //src是源字符串,separator是把src分隔为几段的标记
                StringTokenizer st = new StringTokenizer(src, separator);
                //测试此 tokenizer 的字符串中是否还有更多的可用标记
                while (st.hasMoreTokens())
                {
                        //返回此 string tokenizer 的下一个标记,并将它加入到可变向量中存放
                        v.addElement(st.nextToken());
                }
                return v;//返回向量
        }

        /*
         * 程序的入口
         */
        public static void main(String[] args)
        {
                //根据经验,这条语句只能在第一行
                JFrame.setDefaultLookAndFeelDecorated(true);
                ChooseAward award = new ChooseAward();
                award.setSize(333, 209);
                award.setLocationRelativeTo(null);//居中显示
                award.setVisible(true);
                award.setAlwaysOnTop(true);//置顶
                award.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
}

posted on 2008-08-14 11:29 志遥 阅读(363) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问