⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ernie.java

📁 抽奖程序:可以存取抽奖者的信息以及抽奖结果
💻 JAVA
字号:
//抽奖程序
//可以存取抽奖者的信息以及抽奖结果,可添加或删除抽奖者
//在帮组菜单中添加了程序的使用介绍及对该程序的一些说明
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.Frame;
import java.lang.*;

public class Ernie extends Frame  implements Runnable
{
	static int num,l,t=0;                    //num:随机数; l:参加抽奖的总人数;t:记录获奖次序
	static String filename=new String();		 //文件名(存储抽奖者的信息及抽奖结果)
	static String idcard[]=new String[30];	 //抽奖者的身份证号码
	static String name[]=new String[30];  	 //抽奖者的姓名
	static String s=new String();     			 //为避免重复得奖,在抽出一名获奖者后将其信息与最后一名抽奖者对换,并减少抽奖人数
	static int stopFlag;		                 //标志所按下的键是开始键(1)还是停止键(2)
  Random myrand=new Random();
 
	Frame framemanual=new Frame();     //创建窗口,该窗口没有标题
	Frame frameabout=new Frame();
	Panel p=new Panel();					                //面板 p:容纳整个摇奖界面;pkey:容纳按键;ptext:容纳号码及结果
  Panel pKey=new Panel();
	Panel pText=new Panel();
  Panel p1=new Panel();
  static TextField roll=new TextField();				//roll:滚动的屏幕;
  static TextArea show=new TextArea();   				//show:所有号码的显示;result:结果显示
	static TextArea result=new TextArea();
	Button buttmanual=new Button(" 关闭 ");
	Button buttabout=new Button(" 关闭 ");
  Button keyStart=new Button(" 开始 ");					//开始、结束和退出按钮
  Button keyStop=new Button(" 停止 ");
  Button keyExit=new Button(" 退出 ");
  MenuBar menuebar=new MenuBar();
	Menu open=new Menu("    Open File   ");
	Menu save=new Menu("    Save File   ");
	Menu help=new Menu("    Help   ");
	MenuItem idname1=new MenuItem("Open Idname");				//打开抽奖者的信息文件
	MenuItem idname2=new MenuItem("Save Idname");				//将抽奖者的信息存为文件
	MenuItem result1=new MenuItem("Open Result");				//打开抽奖结果文件
	MenuItem result2=new MenuItem("Save Result");				//将抽奖结果存为文件
	MenuItem manual=new MenuItem("Manual");				//打开抽奖结果文件
	MenuItem about=new MenuItem("About");				//将抽奖结果存为文件
	public Ernie(int k){}
	public Ernie() throws HeadlessException 
	{
		this.setTitle("摇奖程序");    
    this.setMenuBar(menuebar);    
    this.add(p);
    
    menuebar.add(open);
    menuebar.add(save);
    menuebar.add(help);
    open.add(idname1);
    open.add(result1);
    save.add(idname2); 
    save.add(result2);
    help.add(manual); 
    help.add(about);
     
    p.setLayout(new BorderLayout());
    p.add(roll,"North");
    p.add(p1,"Center");
    p1.setLayout(new BorderLayout());
    p1.add(pKey,"North");
    p1.add(pText,"Center"); 
    pKey.setLayout(new FlowLayout(FlowLayout.CENTER,100,10));
    pKey.add(keyStart);
    keyStart.setFont(new Font("Monosapced",0,12));
    pKey.add(keyStop);
    keyStop.setFont(new Font("Monosapced",0,12));
    pKey.add(keyExit);
    keyExit.setFont(new Font("Monosapced",0,12));
    pText.setLayout(new GridLayout(1,2));
    pText.add(show);
    pText.add(result);
    
    this.addWindowListener(new WindowListener(){                                    //关闭窗口
    	public void windowClosing(WindowEvent e){  System.exit(1); }
    	public void windowOpened(WindowEvent e){}
    	public void windowIconified(WindowEvent e){}
    	public void windowDeiconified(WindowEvent e){}
    	public void windowClosed(WindowEvent e){}
    	public void windowActivated(WindowEvent e){}
    	public void windowDeactivated(WindowEvent e){}
    	});
    idname1.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {idname1_actionPerformed(e);}
    	});
    result1.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {result1_actionPerformed(e);}
    	});
    idname2.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {idname2_actionPerformed(e);}
    	});
    result2.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {result2_actionPerformed(e);}
    	});
    manual.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {manual_actionPerformed(e);}
    	});
    about.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {about_actionPerformed(e);}
    	});
    keyStart.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {keyStart_actionPerformed(e);}
    	});
    keyStop.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {keyStop_actionPerformed(e);}
    	});
    keyExit.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e) {keyExit_actionPerformed(e);}
    	});
  }
  
  public void idname1_actionPerformed(ActionEvent e)               //打开文件,将抽奖者的信息放入show文本区中
	{
		JFileChooser jfchooser1=new JFileChooser("E:");
		String str=new String();
	  int i;
		if(e.getSource()==idname1)
		{
			int returnVal=jfchooser1.showOpenDialog(this);
      String filename=new String();
		 	if(returnVal==JFileChooser.APPROVE_OPTION)
		 	{
		 		try
		 		{
		 			filename=jfchooser1.getSelectedFile().getAbsolutePath();
		 			this.setTitle(" open "+ filename);
		 			show.setText("所有参加抽奖人员的信息如下:\n\n");
		 			FileReader fileReader=new FileReader(filename);
		 			BufferedReader bufReader=new BufferedReader(fileReader);
		 			for(i=0;(str=bufReader.readLine())!=null;i++)
		 			{
		 				show.append(str+"\n");
		 				idcard[i]=new String();
		 				name[i]=new String();
		 				idcard[i]=str.substring(0,18);
		 				name[i]=str.substring(18);
		 			}
		 			l=i;
		 			t=0;
		 		}
		   	catch(IOException ie){}
		  }
		} 
  }
          	
	public void result1_actionPerformed(ActionEvent e)               //打开文件,将获奖者的信息放入result文本区中
	{
		JFileChooser jfchooser1=new JFileChooser("E:");
		if(e.getSource()==result1)
		{
			int returnVal=jfchooser1.showOpenDialog(this);
			String filename=new String();
			String str=new String();
			if(returnVal==JFileChooser.APPROVE_OPTION)
			{
				try
				{
					result.setText("");
					filename=jfchooser1.getSelectedFile().getAbsolutePath();
					this.setTitle(" open "+ filename);
					FileReader fileReader=new FileReader(filename);
					BufferedReader bufReader=new BufferedReader(fileReader);
					for(int i=0;(str=bufReader.readLine())!=null;i++)
					{
						result.append(str+"\n");
					}
			  }
			  catch(IOException ie){}	 
			}
	  } 
	}
	
	public void idname2_actionPerformed(ActionEvent e)               //将show文本区中抽奖者的信息保存为文件
	{
		if(e.getSource()==idname2)
		{
			JFileChooser jfchooser2=new JFileChooser("E:");
		  int returnVal=jfchooser2.showSaveDialog(this);
      if(returnVal==JFileChooser.APPROVE_OPTION)			   
			{
				try
				{
					filename=jfchooser2.getSelectedFile().getAbsolutePath();
		   		String str=new String();
		   		String []out=new String[30];
		   		str=show.getText();
		   		str=str.substring(str.indexOf((int)'\n')+1);
		   		str=str.substring(str.indexOf((int)'\n')+1);
			 		FileOutputStream outputfile=new FileOutputStream(filename);
        	DataOutputStream outs=new DataOutputStream(outputfile);
			    for(int i=0;str!=null;i++)
		 			{
		 				out[i]=new String();
		 				out[i]=str.substring(0,str.indexOf((int)'\n'));
		 				outs.write(out[i].getBytes());
		 				outs.writeByte((int)'\r');
		 				outs.writeByte((int)'\n');
		 				str=str.substring(str.indexOf((int)'\n')+1);
		 			}
		 			outs.close();
		   	}
	      catch(Exception ie){}
		  }
		}
	}
	
	public void result2_actionPerformed(ActionEvent e)               //将result文本区中获奖者的信息存为文件
	{
		if(e.getSource()==result2)
		{
			JFileChooser jfchooser2=new JFileChooser("E:");
		  int returnVal=jfchooser2.showSaveDialog(this);
      if(returnVal==JFileChooser.APPROVE_OPTION)			   
			{
				try
				{
					String str=new String();
		   		String res[]=new String[30];
		   		str=result.getText();
		   		filename=jfchooser2.getSelectedFile().getAbsolutePath();
		   		FileOutputStream outputfile=new FileOutputStream(filename);
        	DataOutputStream outs=new DataOutputStream(outputfile);
        	for(int i=0;str.substring(0,str.indexOf((int)'\n')).compareTo("所有获奖人员均已抽取出")!=0;i++)
		 			{
		 				res[i]=new String();
		 				res[i]=str.substring(0,str.indexOf((int)'\n'));
		 				outs.write(res[i].getBytes());
		 				outs.writeByte((int)'\r');
		 				outs.writeByte((int)'\n');
		 				str=str.substring(str.indexOf((int)'\n')+1);
		 			}
			    outs.close();
		   	}
	      catch(IOException ie){}
		  }
		}
	}
	
	public void manual_actionPerformed(ActionEvent e)                 //manual
	{
		if(e.getSource()==manual)
		{
			framemanual.setTitle("manual");          //窗口的标题
			framemanual.setSize(400,250);            //窗口大小
			framemanual.setLocation(20,110);
			framemanual.setVisible(true);	           //显示窗口
			Label l1=new Label();
			Label l2=new Label();
			Label l3=new Label();
			Label l4=new Label();
			Label l5=new Label();
			l1.setForeground(new Color(150, 112, 165));
			l2.setForeground(new Color(150, 112, 165));
			l3.setForeground(new Color(150, 112, 165));
			l4.setForeground(new Color(150, 112, 165));
			l5.setForeground(new Color(150, 112, 165));
	
			l1.setText("程序的使用说明");
			l1.setSize(250,30);
			l1.setLocation(150,35);
			l1.setFont(new Font("Monosapced",0,15));
			framemanual.add(l1);
	
	    l2.setSize(360,20);
			l2.setLocation(30,75);
			l2.setFont(new Font("Monosapced",0,12));
			framemanual.add(l2);
			l2.setText("先载入抽奖者的信息文件,然后点击开始进行抽奖,每点击一次");
			
		  l3.setSize(360,20);
			l3.setLocation(30,95);
			l3.setFont(new Font("Monosapced",0,12));
			framemanual.add(l3);
			l3.setText("停止则会产生一个获奖者,当抽奖结束后可将获奖结果存为文件");
			
			l4.setSize(360,20);
			l4.setLocation(30,115);
			l4.setFont(new Font("Monosapced",0,12));
			framemanual.add(l4);
			l4.setText("若要重新抽奖可再次载入抽奖者信息,开始新一轮的抽奖");
			
			l5.setSize(360,20);
			l5.setLocation(30,135);
			l5.setFont(new Font("Monosapced",0,12));
			framemanual.add(l5);
			l5.setText("若要察看获奖结果可打开获奖结果文件进行察看");
							
			buttmanual.setActionCommand("");
			buttmanual.setSize(60,27);
			buttmanual.setLocation(310,210);
			framemanual.add(buttmanual);
        			
			buttmanual.addActionListener(new java.awt.event.ActionListener()
			{
	   			public void actionPerformed(ActionEvent e){buttmanual_actionPerformed(e);}
       			}); 
    		   	   
           	}		 
      	} 
    
 	public void buttmanual_actionPerformed(ActionEvent e)            //添加manual消失按钮
  	{
	 	if(e.getSource()==buttmanual)
     		framemanual.setVisible(false);
  	}
  	
  	
 public void about_actionPerformed(ActionEvent e)                  //about
	{
		if(e.getSource()==about)
		{
			frameabout.setTitle("about");          //窗口的标题
			frameabout.setSize(400,250);            //窗口大小
			frameabout.setLocation(20,110);
			frameabout.setVisible(true);	           //显示窗口
			Label l1=new Label();
			Label l2=new Label();
			Label l3=new Label();
		  l1.setForeground(new Color(150, 112, 165));
			l2.setForeground(new Color(150, 112, 165));
			
	    l1.setSize(360,20);
			l1.setLocation(110,70);
			l1.setFont(new Font("Monosapced",0,12));
			frameabout.add(l1);
			l1.setText("Pudn.com");
			
		 		
			l2.setSize(360,20);
			l2.setLocation(110,130);
			l2.setFont(new Font("Monosapced",0,12));
			frameabout.add(l2);
			l2.setText("oy127@eyou.com");
						
			buttabout.setActionCommand("");
			buttabout.setSize(60,27);
			buttabout.setLocation(310,210);
			frameabout.add(buttabout);
        			
			buttabout.addActionListener(new java.awt.event.ActionListener()
			{
	   			public void actionPerformed(ActionEvent e){buttabout_actionPerformed(e);}
       			}); 
    		   	   
           	}		 
      	} 
    
 	public void buttabout_actionPerformed(ActionEvent e)            //添加about消失按钮
  	{
	 	if(e.getSource()==buttabout)
     		frameabout.setVisible(false);
  	} 	
	
	public void  keyStart_actionPerformed(ActionEvent e)               //“开始”按钮
	{
		if(e.getSource()==keyStart)
		{
			stopFlag=1;
		}
	}
	
	public void  keyStop_actionPerformed(ActionEvent e)               //“停止”按钮
	{
		if(e.getSource()==keyStop)
		{
			stopFlag=2;
		}
	}
	
	public void  keyExit_actionPerformed(ActionEvent e)               //“退出”按钮
	{
		if(e.getSource()==keyExit)
		{
			System.exit(1);
		}
	}
	
	public static void main(String args[])throws InterruptedException
	{
		Ernie f=new Ernie();
		f.setSize(500,500);
		f.setVisible(true);
		Runnable r=new Ernie(1);
		Thread  g=new Thread(r,"start");                                //创建一个线程
    g.start();                   //启动线程
  }
  
  public void run()                                                 //重写线程体方法
	{
		while(true)
		{
			if(stopFlag==1)
			{
				num=myrand.nextInt(l-t);
				roll.setText(idcard[num]);
      }
      else if(stopFlag==2)
      {
      	t++;
				switch(t)
				{
					case 1:
					result.setText("");
					result.append("一等奖:\n"+"   "+idcard[num]+"  "+name[num]);
					break;
					case 2:
					result.append("\n\n二等奖:\n"+"   "+idcard[num]+"  "+name[num]);
					break;
					case 3:
					case 4:
					result.append("\n   "+idcard[num]+"  "+name[num]);
					break;
					case 5:
					result.append("\n\n三等奖:\n"+"   "+idcard[num]+"  "+name[num]);
					break;
					case 6:
					case 7:
					case 8:
					case 9:
					result.append("\n   "+idcard[num]+"  "+name[num]);
					break;
					default:
					result.append("\n\n所有获奖人员均已抽取出\n");
					t--;
					break;
				}	
				s=idcard[num];
				idcard[num]=idcard[l-t];
				idcard[l-t]=s;
				s=name[num];
				name[num]=name[l-t];
				name[l-t]=s;	
				stopFlag=0;
			}
    }  
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -