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();
	}
}

出现问题:有时在同一个框中显示两条信息,所以在run()中使线程随机sleep。

int m=(int) (Math.random() * 100) + 50;
try {
	Thread.sleep(m);
} catch (Exception e) {
	e.printStackTrace();
}
菜鸟操作,仅供参考......




  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值