Recently, I found a "job" to teach students how to get started with Java. I just talked about some basics. In order to arouse everyone's interest in learning, I wrote a simple roll call machine. Since awt rarely involves it, I found the online version and wrote the List version and the array version respectively. Since I haven't explained the List to students yet, I only learn about arrays, so I have modified an array version separately, Because considering the general type, it can read the txt text at any position, and only need to write it to the students line by line according to the format to achieve roll call. The Random is used to achieve pseudo-random. I can basically understand the code directly.

Array version source code:

 import java.awt. BorderLayout; import java.awt. Color; import java.awt. Container; import java.awt. Dimension; import java.awt. Font; import java.awt.event. ActionEvent; import java.awt.event. ActionListener; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util. ArrayList; import java.util. Arrays; import java.util. List; import java.util. Random; import javax.swing. JButton; import javax.swing. JFrame; import javax.swing. JLabel; import javax.swing. SwingConstants; /** *Simple version Random Roll Counter - Array version *  * @author lcry * */ public class RandomStudent1 { //Read the list and modify the path (txt text file, one student per line) public static String localurl = "H:\\class1.txt"; //Program entry public static void main(String[] args) { RandomStudent1 rn = new RandomStudent1(); rn.init(); } //Main panel JFrame rFrame=new JFrame ("Simple random roll call -- By: Lcry"); //First Name String[] stuName = readclass(localurl); //Label for storing name JLabel name = new JLabel(); //Buttons JButton btn=new JButton ("random roll call"); //Pseudo random numbers are used. You can also skip this. You can find the settings of java random numbers on the Internet Random rd = new Random(); public void init() { //Prompt label page JLabel jt=new JLabel ("random roll call"); //Center Label jt.setHorizontalAlignment(SwingConstants.CENTER); //Set font size Jt.setFont (new Font ("random roll call", 1, 50)); //Set button size btn.setPreferredSize(new Dimension(70, 39)); //Set the label of name display to center name.setHorizontalAlignment(SwingConstants.CENTER); //Implement the listening event of Action button through anonymous class btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Get random names String n = getRandomName(); //Set the text of the name label name.setText(n); //Set Font name.setFont(new java.awt.Font(n, 1, 100)); //Set font color name.setForeground(Color.red); } }); //Get the panel of JFrame Container p = this.rFrame.getContentPane(); //Set the layout method. I use BorderLayout layout p.setLayout(new BorderLayout(3, 1)); //Add prompt label in the north p.add(jt, BorderLayout.NORTH); //Add name tag in the center p.add(name, BorderLayout.CENTER); //Add Button Control in South p.add(btn, BorderLayout.SOUTH); //Resize. It is impossible to set the label size in Java rFrame.pack(); //Set Form Size rFrame.setSize(500, 500); //Setting can display rFrame.setVisible(true); } //Get random names public String getRandomName() { int a = 0; //When the random class implements random numbers, only the upper limit can be set, that is, random numbers generate numbers between 0-stuName.length a = rd.nextInt(stuName.length); System. out. println ("random number:"+a+", name:"+stuName [a]); return stuName[a]; } //Read the class list and store it in the array public static String[] readclass(String localfile) { //Read file BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(localfile)); } catch (FileNotFoundException e) { e.printStackTrace(); } //Directly write out the number of students to read: 100 by default String[] strs = new String[100]; String line; int i = 0; try { while ((line = reader.readLine()) !=  null) { strs[i] = line; i++; } } catch (IOException e) { e.printStackTrace(); } try { reader.close(); } catch (IOException e) { e.printStackTrace(); } int count = 0; for (String string : strs) { if (string != null) { count++; } } //The final student list is the one that is empty. The copyOfRange method is used String[] copyOfRange = Arrays.copyOfRange(strs, 0, count); return copyOfRange; } }

List version source code:

 import java.awt. BorderLayout; import java.awt. Color; import java.awt. Container; import java.awt. Dimension; import java.awt. Font; import java.awt.event. ActionEvent; import java.awt.event. ActionListener; import java.io.BufferedReader; import java.io.FileReader; import java.util. ArrayList; import java.util. List; import java.util. Random; import javax.swing. JButton; import javax.swing. JFrame; import javax.swing. JLabel; import javax.swing. SwingConstants; /** *Simple version Random roll call list version * @author lcry * */ public class RandomStudent2 { //Read the list and modify the path (txt text file, one student per line) public static String localurl = "H:\\class1.txt"; //Program entry public static void main(String[] args) throws Exception { RandomStudent2 rn = new RandomStudent2(); rn.init(); } //Main panel JFrame rFrame=new JFrame ("Simple random roll call -- By: Lcry"); //Label for storing name JLabel name = new JLabel(); //Buttons JButton btn=new JButton ("roll call"); //Generate random number Random rd = new Random(); public void init() throws Exception { //Prompt label page JLabel jt=new JLabel ("random roll call"); //Center Label jt.setHorizontalAlignment(SwingConstants.CENTER); //Set font size Jt.setFont (new Font ("random roll call", 1, 50)); //Set button size btn.setPreferredSize(new Dimension(70, 39)); //Set the label of name display to center name.setHorizontalAlignment(SwingConstants.CENTER); //Implement the listening event of Action button through anonymous class btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Get random names String n; try { n = getRandomName(); //Set the text of the name label name.setText(n); //Set Font name.setFont(new Font(n, 1, 100)); //Set font color name.setForeground(Color.red); } catch (Exception e1) { e1.printStackTrace(); } } }); //Get the panel of JFrame Container p = this.rFrame.getContentPane(); //Set the layout method. I use BorderLayout layout p.setLayout(new BorderLayout(3, 1)); //Add prompt label in the north p.add(jt, BorderLayout.NORTH); //Add name tag in the center p.add(name, BorderLayout.CENTER); //Add Button Control in South p.add(btn, BorderLayout.SOUTH); //Resize. It is impossible to set the label size in Java rFrame.pack(); //Set Form Size rFrame.setSize(500, 500); //Setting can display rFrame.setVisible(true); } //Get random names public String getRandomName() throws Exception { List readclass = readclass(localurl); int nextInt = rd.nextInt(readclass.size()); System. out. println ("Random number:"+nextInt+", name:"+readclass. get (nextInt)); String reString = (String) readclass.get(nextInt); return reString; } //Read the class list and save it in the list public static List readclass(String localfile) throws Exception { //Read file student information BufferedReader reader = new BufferedReader(new FileReader(localfile)); List list = new ArrayList(); String line; while ((line = reader.readLine()) !=  null) { list.add(line); } reader.close(); return list; } }

usage method:

modify localurl It is the path of. txt in your list. Just run the program.
By default, the array version can read up to 100 people. If the class has more than 100 students, you can modify the array length in the array readclass method.

Running screenshot:

 Java implementation of simple random student roll call (with source code)

Article Contents