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("加载数据");
private JMenuItem[] filem={new JMenuItem("选择文件"),new JMenuItem("保存"),new JMenuItem("退出")
};
JTextField t_identNumber = new JTextField();
Vector v_identNumber = 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;
int chooseTime = 0;
int rnum;
int n;
public ChooseAward()
{
super("号码抽取系统");
t_identNumber.setEditable(false);
Container contentPane = getContentPane();
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[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
b_printaward_ActionPerformed(e);
}
});*/
filem[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
b_close_ActionPerformed(e);
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
for(int i=0;i<filem.length;i++)
{
fileMenu.add(filem[i]);
fileMenu.addSeparator();
}
menubar.add(fileMenu);
setJMenuBar(menubar);
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);
p_north.setLayout(new GridLayout(2,1));
p_north.add(l_identNumber);
p_north.add(t_identNumber);
contentPane.add(p_north,BorderLayout.NORTH);
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;
BufferedReader reader = null;
int i = filechooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
l_information.setText("数据加载中,请稍等...");
FileInputStream in = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(in, "utf-8");
reader = new BufferedReader(isr);
while (true)
{
//读取一个文本行
String data = reader.readLine();
System.out.println(data);
if (data == null)
{
l_information.setText("数据加载完成!");
break;
}
Vector v = this.apart(data);
if (v == null)
{
l_information.setText("数据格式不正确,请重新加载!");
return;
}
try
{
v_identNumber.add(k, v.elementAt(0));
k++;
}
catch(Exception e4)
{
System.out.println("格式中没有分隔符号出现的错误");
l_information.setText("导入的数据格式错误!");
break;
}
}
} catch (Exception ex)
{
ex.printStackTrace(); // 输出出错信息
}
}
}
/*
* 启动按钮的事件
*/
public void b_start_ActionPerformed(ActionEvent e)
{
//判断存储两个标记的向量中是否为空
if (v_identNumber.size() <= 0)
{
l_information.setText("数据没有加载,请加载数据!");
}
else
{
if (chooseTime >1)
{
l_information.setText("为了确保所有用户的随机性,请重新启动程序!");
}
else//执行....
{
awardThread = new ChooseThread(this);
awardThread.changeflag_start();
l_information.setText("将产生:20个随机号码");
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="";
Random r=new Random();
if(chooseTime > 1){
JOptionPane.showMessageDialog(ChooseAward.this, "为了确保所有用户的随机性,请重新启动程序");
b_start.setText("开始");
awardThread.changeflag_stop();
}
else{
for(rnum=19;rnum>-1;rnum--)
{
n=r.nextInt(rnum+1);
String ss=(String)v_identNumber.get(n);
v_printident.addElement(ss + "\r\n");
v_identNumber.remove(n);
}
l_identNumber.setText("被选中");
b_start.setText("继续");
message = "20个随机号码已被抽中";
JOptionPane.showMessageDialog(this, message);
try
{
FileOutputStream fs_out = new FileOutputStream("D:\\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).toString().trim() + "\r\n");
}
out.close();
l_information.setText("文件输出成功!保存在D:\\result.txt");
} catch (FileNotFoundException fe)
{
System.err.println(fe);
} catch (IOException ioe)
{
System.err.println(ioe);
}
b_start.setEnabled(true);
b_stop.setEnabled(false);
}
}
public void b_printaward_ActionPerformed(ActionEvent e)
{
try
{
FileOutputStream fs_out = new FileOutputStream("D:\\result.txt");
DataOutputStream out = new DataOutputStream(fs_out);
for (int i = 0; i < v_printident.size(); i++)
{
System.out.println(v_printident.elementAt(i));//测试用
String outprint = (String) v_printident.elementAt(i);
out.writeUTF(new String(outprint.getBytes("gb2312")) + "\r\n");
}
out.close();
l_information.setText("文件输出成功!保存在D:\\result.txt");
} catch (FileNotFoundException fe)
{
System.err.println(fe);
} catch (IOException ioe)
{
System.err.println(ioe);
}
}
public void b_close_ActionPerformed(ActionEvent e)
{
System.exit(0);
}
public Vector apart(String src)
{
Vector<String> v = new Vector<String>();
v.addElement(src);
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);
}
}
class ChooseThread extends Thread
{
private boolean runFlag = true;
private ChooseAward chooseAward = null;
评论0