java抽奖程序源码及心得
代码 package cj;
importjava.awt.*;
importjavax.swing.*; importjava.awt.event.*; importjava.util.*; importjava.util.Timer; import java.io.*;
classlmChouJiang {
JFrame frame = new JFrame();
JLabellabel_name = new JLabel(\"姓名\"); JLabellabel_num = new JLabel(\"学号\"); JPanel jp1 = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); public static int q = 0;
JButtonstartButton = new JButton(\"开始抽奖\"); JButtonstopButton = new JButton(\"结束抽奖\"); int index; // 保持学生信息与头像的同步
ArrayList publiclmChouJiang() { // 读取学生信息 array = new ArrayList try { BufferedReaderbufferReader = new BufferedReader(new FileReader( \"C:\\\\Users\\\\qxu\\\\Desktop\\\\stu2.txt\")); String lineContent = null; String name = \"\"; intnum = 0; while ((lineContent = bufferReader.readLine()) != null) { // 每一行区分的标识符是空格 StringTokenizerst = new StringTokenizer(lineContent, \" \"); // 读取学号和姓名。每一行只有两个元素,所以I的取值就是0和1,0代表学号,1代表姓名 for (inti = 0; st.hasMoreElements(); i++) { String word = (String) st.nextElement(); if (i == 0) { num = Integer.parseInt(word); } else { } } } name = word; i--; Student student = new Student(name, num); array.add(student); } catch (IOException e) { } /* frame.getContentPane().add(BorderLayout.WEST, startButton); frame.getContentPane().add(BorderLayout.EAST, stopButton); frame.getContentPane().add(BorderLayout.NORTH, label_num); frame.getContentPane().add(BorderLayout.CENTER, label_name); */ frame.setLayout(new GridLayout(3,1)); jp1.add(label_num); jp2.add(label_name); jp3.add(startButton); jp3.add(stopButton); frame.setSize(600, 600); e.printStackTrace(); frame.setVisible(true); frame.add(jp1); frame.add(jp2); frame.add(jp3); startButton.addActionListener(new startlButtonListener()); stopButton.addActionListener(new endButtonListener()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } index = 0; classMyTask extends TimerTask { public void run() { int end = (int) (Math.random() * array.size()); System.out.println(\"end = \" + end); label_num.setText(\"学号:\" + String.valueOf(array.get(end).getNum())); label_name.setText(\"姓名:\" + array.get(end).getName()); frame.repaint(); try { Thread.sleep(5); } catch (Exception e) { } e.printStackTrace(); } } classstartlButtonListener implements ActionListener { } public void actionPerformed(ActionEvent event) { } if(q == 0){ } timer.scheduleAtFixedRate(new MyTask(), 0, 100); timer = new Timer(); q ++; classendButtonListener implements ActionListener { } public void actionPerformed(ActionEvent event) { } timer.cancel(); if(q > 0){ } q --; classjPanel extends JPanel { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { Image image = new ImageIcon(\"image/\" + index + \".jpg\").getImage(); } class Student { publicintgetNum() { returnnum; public void setName(String name) { } this.name = name; public String getName() { } return name; private String name; privateintnum; } } g.drawImage(image, 60, 100, this); } } public void setNum(intnum) { } this.num = num; public Student(String name, intnum) { } this.name = name; this.num = num; public void print() { } System.out.println(num + \" \" + name); public class choujiang { } public static void main(String[] args) { } lmChouJiangcj = new lmChouJiang(); 因篇幅问题不能全部显示,请点此查看更多更全内容