Write the program, in advance all the audience names into the array, and then get the total number of elements of the array, and finally in the elements of the array randomly sampling the subscript, according to the extracted subscript to obtain the name of the lucky audience.
Ideas are as follows:
Defines the key event for the input box, using the KeyEvent class's Getkeychar () function to determine whether it is a carriage return character, or not;
Use the IsEmpty () function to determine if there is a string in the text box, and if there is no string, no processing;
If the legal input is through the JTextArea class's Append () method, the input person name and the carriage return character are added to the personnel list;
Use the SelectAll () method to select all characters in the text box;
Define the function to be executed when clicking on the "Extract" button, get the person list text by the GetText () method of the JTextArea class, and deposit the string;
Creates a one-dimensional array of strings, which is separated by a return character and then stored in the array;
A random array index is generated by Math.random (), which is used as an array subscript of the winners;
Define the winning information that contains the format parameters;
Add personnel parameters to the winning information by using 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 execute when clicking the "Exit" button, and use the System.exit (0) method to exit the program.
The code is as follows:
Copy 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 array random sampling of 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 ')/not a carriage return character does not handle
Return
String name = Textfield.gettext ();
if (Name.isempty ())//If the text box does not have a string to handle
Return
Textarea_1.append (name + "\ n");//Add the person name and carriage return to the People list
Textfield.selectall ()//Select text box all characters
}
protected void do_button_actionperformed (ActionEvent e) {
String perstring = Textarea_1.gettext ();//Get people list text
string[] Personnelarray = Perstring.split ("\n{1,}");//Get people array
int index = (int) (Math.random () * personnelarray.length);//Generate Random array index
Define winning information that contains format parameters
String Formatarg = "This time draws the audience personnel: \n\t%1$s\n congratulates%1$5s to become this audience Lottery award winner." "
+ "\ n \ nyou will be issued for%1$5s: \n\t 20 cartons of expired yogurt." ";
Add personnel parameters for winning information
String info = String.Format (Formatarg, Personnelarray[index]);
Textarea.settext (info);//show winning information in text field
}
protected void do_button_1_actionperformed (ActionEvent e) {
System.exit (0);
}
}
The effect is as shown in the figure: