Java Sixth time job

Source: Internet
Author: User

Import java.awt.*;

Import javax.swing.*;

Import java.awt.event.*;

Import java.util.*;

Import Java.util.Timer;

Import java.io.*;

Class Choujiang {

JFrame frame = new JFrame ();

JLabel label_name = new JLabel ("name");

JLabel label_num = new JLabel ("study number");

JPanel photo = new JPanel ();

JButton Startbutton = new JButton ("Start Lottery");

JButton Stopbutton = new JButton ("End Draw");

int index; Keep student information in sync with avatar

Arraylist<student> array = null; Store read-Out student information

Timer timer;

Public Choujiang () {

Read Student Information

Array = new arraylist<student> ();

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) {

Each row distinguishes between identifiers that are spaces

StringTokenizer st = new StringTokenizer (linecontent, "");

Read the school number and name. Each line has only two elements, so I's value is 0 and 1, 0 for the school number, 1 for the name

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);

}

} catch (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 (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 () {

int end = (int) (Math.random () * array.size ());

for (int i = 0; i < end; i++) {

Abel_num

. SetText ("School Number:" + string.valueof (Array.get (i). Getnum ()));

Label_name.settext ("Name:" + array.get (i). GetName ());

index = i;

Frame.repaint ();

try {

Thread.Sleep (5);

} catch (Exception e) {

E.printstacktrace ();

}

}

}

}

Class Startlbuttonlistener implements 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 serialversionuid = 1L;

public void Paintcomponent (Graphics g) {

Image image = new ImageIcon ("image/" + index + ". jpg"). GetImage ();

G.drawimage (image, Max, 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) {

Choujiang CJ = new Choujiang ();

}

}

Java Sixth time job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.