Write a program to input the names of all the viewers into the array in advance, then obtain the total number of array elements, and finally randomly extract the subscripts of the elements from the array elements, and obtain the names of the lucky viewers based on the extracted subscripts.
The idea is as follows:
Define the key event of the input box, and use the getKeyChar() function of the KeyEvent class to determine whether it is a carriage return character. If not, it will not be processed;
Use the isEmpty() function to determine whether there is a string in the text box. If there is no string, no processing will be performed;
If it is a legal input, add the input person's name and carriage return character to the personnel list through the append() method of the JTextArea class;
Use the selectAll() method to select all characters in the text box;
Define the function to be executed when the "Extract" button is clicked, obtain the personnel list text through the getText() method of the JTextArea class, and store it in a string;
Create a one-dimensional string array, split the previous string according to the carriage return character and store it in the array;
Generate a random array index through Math.random() as the array subscript of the winner;
Define winning information including format parameters;
Add personnel parameters to the winning information through the format() method of the String class;
Use the setText() method of the JTextArea class to display the winning information in the text field;
Define the function to be executed when the "Exit" button is clicked, and use the System.exit(0) method to exit the program.
The code is as follows:
Copy the code code as follows:
package cn.edu.xidian.crytoll;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ArrayExample {
private JFrame frame;
private JTextField textField;
private JScrollPane scrollPane;
private JLabel label_1;
JTextArea textArea = new JTextArea();
private JTextArea textArea_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArrayExample window = new ArrayExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ArrayExample() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("Use the array to randomly select lucky viewers");
frame.setBounds(100, 100, 500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("/u8F93/u5165/u5728/u573A/u89C2/u4F17/u59D3/u540D/u6309/u56DE/u8F66");
label.setBounds(10, 10, 132, 15);
frame.getContentPane().add(label);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
do_textField_keyPressed(e);
}
});
textField.setBounds(10, 35, 132, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 66, 132, 185);
frame.getContentPane().add(scrollPane);
textArea_1 = new JTextArea();
scrollPane.setViewportView(textArea_1);
label_1 = new JLabel("/u9009/u53D6/u89C2/u4F17/u4EBA/u5458/uFF1A");
label_1.setBounds(180, 10, 132, 15);
frame.getContentPane().add(label_1);
textArea.setBounds(180, 34, 214, 217);
frame.getContentPane().add(textArea);
JButton button = new JButton("/u62BD/u53D6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(404, 187, 70, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("/u9000/u51FA");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_1_actionPerformed(e);
}
});
button_1.setBounds(404, 228, 70, 23);
frame.getContentPane().add(button_1);
}
protected void do_textField_keyPressed(KeyEvent e) {
if (e.getKeyChar() != '/n')// If it is not a carriage return character, it will not be processed.
return;
String name = textField.getText();
if (name.isEmpty())// If there is no string in the text box, no processing will be done
return;
textArea_1.append(name + "/n");//Add the entered name and carriage return character to the list of people
textField.selectAll();//Select all characters in the text box
}
protected void do_button_actionPerformed(ActionEvent e) {
String perstring = textArea_1.getText();//Get the text of the personnel list
String[] personnelArray = perstring.split("/n{1,}");//Get the personnel array
int index = (int) (Math.random() * personnelArray.length); // Generate a random array index
//Define winning information including format parameters
String formatArg = "The audience members for this draw:/n/t%1$s/nCongratulations to %1$5s for becoming the grand prize winner of this audience draw."
+ "/n/nWe will award %1$5s:/n/t20 boxes of expired yogurt.";
//Add personnel parameters for winning information
String info = String.format(formatArg, personnelArray[index]);
textArea.setText(info);//Display winning information in the text area
}
protected void do_button_1_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
The effect is as shown in the figure: