/* (Start of Program head Note) * Copyright and version Statement of the program (c) 2011, the Computer College of Yantai University * All rights reserved. * File Name: * Author: Shangpeng * Date of Completion: August 13, 2013 * Version number: 001 * for tasks And the description of the solution method * Input Description: * Problem Description: Using Eclipse's Windowbuilder of the written window program, random sampling of Lucky viewers, the interface through drag-and-drop generation, triggering events are their own written code, the rest of the code is automatically generated * program output:
First of all the audience name to generate an array, and finally get the total number of arrays, and then randomly sampled in the name array subscript, according to the array subscript to get the lucky audience * program Head of the annotation end/Package chapter III array;
Import Java.awt.BorderLayout;
public class extracts lucky viewers extends JFrame {private JPanel contentpane;
Private JTextField TextField;
Private JTextArea TextArea;
Private JTextArea Resultarea;
/** * Launch the application.
*/public static void main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {
Draw the lucky viewer frame = new to draw the lucky audience ();
Frame.setvisible (TRUE);
catch (Exception e) {e.printstacktrace ();
}
}
});
}/** * Create the frame.
* * Public extraction of Lucky viewers () {setdefaultcloseoperation (jframe.exit_on_close); SetBounds (100, 100, 450, 300);
ContentPane = new JPanel ();
Contentpane.setborder (New Emptyborder (5, 5, 5, 5));
Setcontentpane (ContentPane);
Contentpane.setlayout (NULL);
TextField = new JTextField (); Textfield.addkeylistener (New Keyadapter () {//@Override public void keypressed (KeyEvent e) {if (E.getkeychar ()!=
' \ n ') {//Not return character not processed;
String name = Textfield.gettext ();
if (Name.isempty ())//If the text box is empty and does not handle return; Textarea.append (name+ "\ n"); Add the input name and carriage return to the text field of the person list textfield.selectall ();
Select all contents of the text Box}});
Textfield.setbounds (10, 31, 132, 21);
Contentpane.add (TextField);
Textfield.setcolumns (10);
JLabel Lblnewlabel = new JLabel ("\u8f93\u5165\u73b0\u573a\u89c2\u4f17\u59d3\u540d\u5e76\u56de\u8f66");
Lblnewlabel.setbounds (10, 10, 153, 15);
Contentpane.add (Lblnewlabel);
TextArea = new JTextArea ();
Textarea.setlinewrap (TRUE);
Textarea.setbounds (10, 79, 132, 173);
Contentpane.add (TextArea); JLabel label =New JLabel ("\u5e78\u8fd0\u89c2\u4f17");
Label.setbounds (203, 10, 54, 15);
Contentpane.add (label);
Resultarea = new JTextArea ();
Resultarea.setlinewrap (TRUE);
Resultarea.setbounds (203, 35, 132, 217);
Contentpane.add (Resultarea);
JButton Btnnewbutton = new JButton ("\u62bd\u53d6"); Btnnewbutton.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {String Perstri ng = Textarea.gettext (); Gets the text of the person list string[] Personarray = perstring.split ("\ n");//split string, generate string array int index = (int) (Math.random () *personarr Ay.length); Generate random array index String FORMATARG = "This time the audience: \n\t%1$s\n Congratulations to%1 $ s to become the winner of this lucky audience." "+" \ n \ NAND We will issue the jackpot for%1 $ s: \ n Expired yogurt 20 cartons ";//define Jackpot information containing format parameters String info = String.Format (Formatarg, Personarray[index]);
/Add the Personnel parameter Resultarea.settext (info) to the winning information;//display the winning information in the Text field}});
Btnnewbutton.setbounds (345, 162, 72, 23);
Contentpane.add (Btnnewbutton);
JButton button = new JButton ("\U9000\U51FA"); Button.addactIonlistener (new ActionListener () {public void actionperformed (ActionEvent e) {Syst Em.exit (0);
Exit Button}});
Button.setbounds (345, 205, 72, 23);
Contentpane.add (button); }
}