package test;
/**
* @(#)Choujiang.java
*
*
* @author Liling
* @version 1.00 2009/12/31
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.*;
import java.util.*;
public class ChouJiang extends JFrame implements ActionListener,Runnable{
Thread t;
private boolean option = false;
private JTextField field;
private JMenuItem jmi_start;
private JMenuItem jmi_stop;
private JMenuItem jmi_exit;
public ChouJiang(){
this.setTitle("抽奖机");
this.setSize(500,100);
this.setLocation(300,300);
this.setLayout(new FlowLayout());
this.setVisible(true);
this.init();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
this.t = new Thread(this);
if("开始".equals(arg) || e.getSource() == this.jmi_start){
this.option = true;
t.start();
}else if("终止".equals(arg) || e.getSource() == this.jmi_stop){
this.option = false;
t = null;
}else if(e.getSource() == this.jmi_exit){
//System.exit(0);
this.dispose();
}
}
private void init(){
this.add(getMPane("Menu"));
this.add(getCPane("抽奖"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
java.util.List<String> li = new ArrayList<String>(); //实现一个List
String line;
File f = new File("d:\\id.txt");
try{
//将文件内容读入到输入流
FileInputStream fis=new FileInputStream(f);
//将输入流数据读入缓冲流
BufferedInputStream bus=new BufferedInputStream(fis);
//用缓冲流的reader方法读出流中数据
BufferedReader reader=new BufferedReader(new InputStreamReader(bus));
line = reader.readLine();
//用while循环接受下一记录,直到没有为空为止
//Choujiang.num = 0;
while (line != null){
li.add(line);//把读入的一行数据保存到List
line = reader.readLine();//读入下一行
}
/****一般形式循环****
for(int i=0;i<li.size();i++)
{
if(this.option == true){
try{
this.field.setText(li.get(i).toString());
t.sleep(60);
if(i == (li.size()-1)) i = 0;
}catch(Exception e){
}
}else{
break;
}
}*/
/*******利用迭代器遍历ArrayList********/
for(ListIterator<String> it = li.listIterator(); it.hasNext();){
if(this.option == true){
try{
this.field.setText(it.next());
t.sleep(60);
if(!it.hasNext()) it = li.listIterator();
}catch(Exception e){
}
}else{
break;
}
}
//关闭打开的有关流
reader.close();
bus.close();
fis.close();
}catch(IOException e){
}
}
public JPanel getMPane(String name){
JPanel jp = new JPanel();
jp.setName(name);
jp.add(getJMenuBar("Menu"));
return jp;
}
public JMenuItem getJMenuItem(String name){
JMenuItem jmi = new JMenuItem(name,KeyEvent.VK_F);
jmi.addActionListener(this);
return jmi;
}
public JMenu getJMenu(String name){
JMenu jm = new JMenu();
jm.setName(name);
jm.setText(name);
this.jmi_start = getJMenuItem("开始");
this.jmi_stop = getJMenuItem("终止");
this.jmi_exit = getJMenuItem("退出");
jm.add(this.jmi_start);
jm.add(this.jmi_stop);
jm.add(this.jmi_exit);
return jm;
}
public JMenuBar getJMenuBar(String name){
JMenuBar jmb = new JMenuBar();
jmb.setName(name);
jmb.add(getJMenu("编辑"));
return jmb;
}
public JPanel getCPane(String name){
JPanel jp = new JPanel();
jp.setName(name);
this.field = getJTextField();
jp.add(this.field);
jp.add(getJButton("开始"));
jp.add(getJButton("终止"));
return jp;
}
public JTextField getJTextField(){
JTextField jtf = new JTextField("",20);
return jtf;
}
public JButton getJButton(String name){
JButton jb = new JButton(name);
jb.setText(name);
jb.addActionListener(this);
return jb;
}
public static void main(String[] args){
new ChouJiang();
}
}