“This is the 27th day of my participation in the August Gwen Challenge, for more details: August Gwen Challenge” juejin.cn/post/698796…


1. Program effect

! [insert picture description here] (img – blog. Csdnimg. Cn / 20200304131… = 400 x) remember before class, the teacher can write with his classmates to answer the question of an extractor extraction, was thinking I also want to do a, have nothing to do during the holiday, self-study Java, idle bored, I also write a, but write not good teacher, ha ha, okay, said the train of thought, decorate the interface first, then start to realize the function of each button, It’s not that hard.


Two. Need to use the package

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
Copy the code

Code 3.

1. Related instance objects, so objects are global objects

 private static JLabel jl= new JLabel("Document:");
 private static JTextField jt =new JTextField();
 private static JButton OpenButton =new JButton("Select file");
 private static JTextField jt2 =new JTextField();// The text box displays the extraction list
 private static JButton StartButton =new JButton("Start drawing");
 private static JLabel ClassjL= new JLabel("Class:");
 private static JTextField ClassjT =new JTextField();// Display the class
 private static JLabel NumjL= new JLabel("Number of people:");
 private static JTextField NumjT =new JTextField();// Display the number of people
 private static JLabel jl2= new JLabel("Extraction mode:");
 private static JComboBox jc= new JComboBox();// Drop down list box
 private static JButton AboutButton =new JButton("About");
 private static JOptionPane jo =new JOptionPane();// A dialog box is displayed
 private static String[]s ;// Use to store the name of a person
 private static Font font = new Font("宋体",Font.BOLD,18); // Set the font object
 private static int Number=0;// The number of people to draw
Copy the code

2. Create a window and initialize the component

    private void windows(a) {
     JFrame jf =new JFrame ("Dog Extractor myself blog :fdogcsdn.com");
     jf.setIconImage(new ImageIcon("Icon.jpg").getImage());
     Container c=jf.getContentPane();
     c.setLayout(new GridLayout(4.2.10.10)); 
     OpenButton.setFocusPainted(false);
     StartButton.setFocusPainted(false);
     AboutButton.setFocusPainted(false);// Remove the dotted box next to the button text
     JPanel jp1 =new JPanel();
     JPanel jp2 =new JPanel(new BorderLayout());
     JPanel jp3 =new JPanel();
     JPanel jp4 =new JPanel();// Add panel
     jt.setColumns(10);
     ClassjT.setColumns(6);
     NumjT.setColumns(4);
     jt2.setHorizontalAlignment(JTextField.CENTER);
     jc.addItem("-- please select --");
     jc.addItem("Draw one person");
     jc.addItem("Draw three.");
     jc.addItem("Draw five.");
     jp1.add(jl);
     jp1.add(jt);
     jp1.add(OpenButton);
     jp2.add(jt2,BorderLayout.CENTER);
     jp3.add(ClassjL);
     jp3.add(ClassjT);
     jp3.add(NumjL);
     jp3.add(NumjT);
     jp3.add(jl2);
     jp3.add(jc); 
     jp4.add(StartButton);
     jp4.add(AboutButton);
     c.add(jp1);
     c.add(jp2);
     c.add(jp3);
     c.add(jp4);
     jf.setVisible(true);
     jf.setBounds(800.200.400.500);
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     getOpenButton(); // The following three methods are used to listen for button event methods
     getAboutButton();
     getSrartButton();
    }
Copy the code

3. Add “Open file” button listening event:

    private void getOpenButton(a) {
     OpenButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          JFileChooser fc =new JFileChooser();// This object is the file selector when we click open file
          fc.setCurrentDirectory(new File("."));// Specify the current default directory
          fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// You can choose to open only files or folders
          fc.setMultiSelectionEnabled(false);// Whether multiple files are allowed
          int i =fc.showOpenDialog(getContentPane());
          if(i==JFileChooser.APPROVE_OPTION) {// Determine whether to open it
           File file =fc.getSelectedFile();
           // Displays the selected content
           jt.setText(fc.getSelectedFile().getName());
           try{
            FileReader fr =new FileReader(file);
            BufferedReader  in =new BufferedReader (fr);
            String line= in.readLine();// Read the contents of the TXT file
            s =line.split("");// Store the name of a person, separated by a space
            NewMessage();   // Listen on events
            getjcomboBox();// Listen on events
           }catch(Exception e1) { e1.printStackTrace(); }}}}); }Copy the code

4. The About button listens for events


    private void getAboutButton(a) {
     AboutButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jo.showMessageDialog(null."TXT file can be created :\n write the class name and space the student name \n name and name must be separated by a space \n can identify the class name and number of students and the list"); }}); }Copy the code

5. Select a listening event from the drop-down list box


    private void getjcomboBox(a) {
     jc.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
       if(e.getStateChange() == ItemEvent.SELECTED) {
        String itemSize = (String) e.getItem();  
        if(itemSize=="Draw one person") {
         Number =1;
        }
        if(itemSize=="Draw three.") {
         Number =3;
        }
        if(itemSize=="Draw five.") {
         Number =5; }}}}); }Copy the code

6. The “Start Extraction” button listens for events


   private void getSrartButton(a) {
     StartButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       switch(Number) {
       case 1:
        int num1 =(int)1+ (int)(Math.random()*(s.length-1-1));
        jt2.setText(s[num1]);
        break;
       case 3:
        int []num3=new int[100];
        for(int i=0; i<100; i++) { num3[i]=(int)1+ (int)(Math.random()*(s.length-1-1));
        }
        for(int i=0; i<98; i++) {if(num3[i]! =num3[i+1] && num3[i]! =num3[i+2] && num3[i+1]! =num3[i+2]) {
             String strtext1= s[num3[i]]+""+s[num3[i+1]] +""+s[num3[i+2]];
             jt2.setText(strtext1);
          break; }}break;
       case 5:
        int []num5=new int[100];
        for(int i=0; i<100; i++) { num5[i]=(int)1+ (int)(Math.random()*(s.length-1-1));
        }
        for(int i=0; i<95; i++) {if(num5[i]! =num5[i+1] && num5[i]! =num5[i+2] && num5[i]! =num5[i+3] && num5[i]! =num5[i+4]
         && num5[i+1]! =num5[i+2] && num5[i+1]! =num5[i+3] && num5[i+1]! =num5[i+4]
         &&num5[i+2]! =num5[i+3] && num5[i+2]! =num5[i+4]
         &&num5[i+3]! =num5[i+4]) {
             String strtext1= s[num5[i]]+""+s[num5[i+1]] +""+s[num5[i+2]] +""+s[num5[i+3]] +""+s[num5[i+4]];
             jt2.setText(strtext1);
          break; }}break; }}}); }Copy the code

7. If the file is opened and read successfully, the text box displays the content

    private void NewMessage(a) {
     ClassjT.setText(s[0]);
     String s1="";
     s1=""+(s.length-1);
     NumjT.setText(s1);
     jt2.setFont(font);
     jt2.setForeground(Color.blue);
     jt2.setText("Ready, please begin extraction.");
    }
Copy the code

8. The main method

 public static void main(String[] args) {
  callmy call = new callmy();
  call.windows();
 }
Copy the code

9. Resource download

You can download the source code from my resources

If there are mistakes, welcome to criticize, welcome to comment;

== every article sentence: in the world, there are difficult and intentional, there will be success and failure. Success depends not on how hard things are, but on how hard you work. Throughout the ages, those who are lazy and do not want to make progress want to succeed, it is simply impossible. But hard-working people, with hard efforts, overcome numerous difficulties, often can succeed. = =