Array example: Use arrays to randomly extract lucky viewers

Source: Internet
Author: User

Running interface:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/143345A94-0.jpg "title =" dos21_ha_08p_tgcatw7k0dn.jpg "alt =" 130458484.jpg"/>

In addition to interface processing, nameArea requires the input and extracts all the characters that meet the requirements to the personalArea area, obtains the viewer list text, and performs Array Processing, finally, a lucky audience is randomly generated.

The detailed code and comments are as follows:

Package com. lixiyu; 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. UIManager; import javax. swing. border. emptyBorder; import javax. swing. border. titledBorder; public class SecondArray extends JFrame {private JPanel contentPane; private JTextField nameArea; private JTextArea personalArea; private JTextArea resultArea; public static void main (String [] args) {try {UIManager. setLookAndFeel ("com. sun. java. swing. plaf. nimbus. nimbusLookAndFeel "); // appearance} catch (Thr Owable e) {e. printStackTrace ();} EventQueue. invokeLater (new Runnable () {public void run () {try {SecondArray frame = new SecondArray (); frame. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}});} public SecondArray () {setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,498,300); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5); setContentPane (conten TPane); setTitle ("using arrays to randomly extract lucky viewers"); contentPane. setLayout (null); JPanel panel = new JPanel (); panel. setBorder (new TitledBorder (null, "Enter the audience and press ENTER", TitledBorder. LEADING, TitledBorder. TOP, null, new Color (59, 59, 59); panel. setBounds (10, 10,174,242); // the first two are coordinates, and the last two are width and height contentPane. add (panel); panel. setLayout (new BorderLayout (0, 5); // layout manager nameArea = new JTextField (); nameArea. addKeyListener (new KeyAd Apter () {public void keyPressed (KeyEvent e) {do_textField_keyPressed (e) ;}}); panel. add (nameArea, BorderLayout. NORTH); nameArea. setColumns (10); JScrollPane scrollPane = new JScrollPane (); panel. add (scrollPane); personalArea = new JTextArea (); personalArea. setEditable (false); scrollPane. setViewportView (personalArea); JPanel panel_1 = new JPanel (); panel_1.setBorder (new TitledBorder (null, "select audience members: ", TitledBorder. LEADING, TitledBorder. TOP, null, new Color (59, 59, 59); panel_1.setBounds (183, 10,219,242); contentPane. add (panel_1); partition (new BorderLayout (0, 0); JScrollPane scrollPane_1 = new partition (); panel_1.add (scrollPane_1); resultArea = new JTextArea (); resultArea. setEditable (false); resultArea. setLineWrap (true); scrollPane_1.setViewportView (resultArea); JButton butt On = new JButton ("extract"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_actionreceivmed (e) ;}}); button. setBounds (407,164, 63, 25); contentPane. add (button); JButton button_1 = new JButton ("exit"); button_1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_?action=med (e );}}); button_1.setBou Nds (407,215, 63, 25); contentPane. add (button_1); // the preceding two buttons are set to extract and return keys} protected void do_textField_keyPressed (KeyEvent e) {if (e. getKeyChar ()! = '\ N') // return is not processed if it is not a carriage return character; String name = nameArea. getText (); if (name. isEmpty () // if the text box does not contain strings, return is not processed; personalArea. append (name + "\ n"); // Add the input name and carriage return to the nameArea in the personnel list. selectAll (); // select all characters in the text box}/* and above are restrictions on the word input characters and added to the personalArea */protected void do_button_actionreceivmed (ActionEvent e) {String perstring = personalArea. getText (); // get the personnel list text String [] personnelArray = perstring. split ("\ n {1,}"); // gets the personnel Array Int index = (int) (Math. random () * personnelArray. length); // generate a random array index // define the winning information containing the format parameter String formatArg = "this audience extraction: \ n \ t % 1 $ s \ n congratulations % 1 $ 5s became the winner of this lottery. "+" \ N we will issue a certificate for % 1 $ s **: \ n \ t iphone iphoneiphoneiphone5s "; // Add the personnel parameter String info = String for the winning information. format (formatArg, personnelArray [index]); resultArea. setText (info); // display the winning information in the text field} protected void do_button_?action=med (ActionEvent e) {dispose ();}}


This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1303995

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.