《最新java抽奖程序源码及心得.docx》由会员分享,可在线阅读,更多相关《最新java抽奖程序源码及心得.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datejava抽奖程序源码及心得java抽奖程序源码及心得java抽奖程序源码及心得代码package liangmiao;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;import java.util.Timer;import java.io.*;clas
2、s lmChouJiang JFrame frame = new JFrame();JLabel label_name = new JLabel(姓名);JLabel label_num = new JLabel(学号);jPanel photo = new jPanel();JButton startButton = new JButton(开始抽奖);JButton stopButton = new JButton(结束抽奖);int index; / 保持学生信息与头像的同步ArrayList array = null; / 存储读出来的学生信息Timer timer;public lm
3、ChouJiang() / 读取学生信息array = new ArrayList();try BufferedReader bufferReader = new BufferedReader(new FileReader(student/stu2.txt);StringBuffer stringBuffer = new StringBuffer();String lineContent = null;String name = ;int num = 0;while (lineContent = bufferReader.readLine() != null) / 每一行区分的标识符是空格St
4、ringTokenizer st = new StringTokenizer(lineContent, );/ 读取学号和姓名。每一行只有两个元素,所以I的取值就是0和1,0代表学号,1代表姓名for (int i = 0; st.hasMoreElements(); i+) String word = (String) st.nextElement();if (i = 0) num = Integer.parseInt(word); else name = word;Student student = new Student(name, num);array.add(student); ca
5、tch (IOException e) e.printStackTrace();frame.getContentPane().add(BorderLayout.WEST, startButton);frame.getContentPane().add(BorderLayout.EAST, stopButton);frame.getContentPane().add(BorderLayout.NORTH, label_num);frame.getContentPane().add(BorderLayout.SOUTH, label_name);frame.getContentPane().add
6、(BorderLayout.CENTER, photo);startButton.addActionListener(new startlButtonListener();stopButton.addActionListener(new endButtonListener();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 400);frame.setVisible(true);index = 0;class MyTask extends TimerTask public void run() in
7、t end = (int) (Math.random() * array.size();for (int i = 0; i end; i+) label_num.setText(学号: + String.valueOf(array.get(i).getNum();label_name.setText(姓名: + array.get(i).getName();index = i;frame.repaint();try Thread.sleep(5); catch (Exception e) e.printStackTrace();class startlButtonListener implem
8、ents ActionListener public void actionPerformed(ActionEvent event) timer = new Timer();timer.scheduleAtFixedRate(new MyTask(), 0, 100);class endButtonListener implements ActionListener public void actionPerformed(ActionEvent event) timer.cancel();class jPanel extends JPanel private static final long
9、 serialVersionUID = 1L;public void paintComponent(Graphics g) Image image = new ImageIcon(image/ + index + .jpg).getImage();g.drawImage(image, 60, 100, this);class Student private String name;private int num;public String getName() return name;public void setName(String name) this.name = name;public int getNum() return num;public void setNum(int num) this.num = num;public Student(String name, int num) this.name = name;this.num = num;public void print() System.out.println(num + + name);public class choujiang public static void main(String args) lmChouJiang cj = new lmChouJiang();-