Java simulates a lottery program

Source: Internet
Author: User

Today using a program to simulate a lottery program that randomly extracts 7 numbers from 1-32
* Need to use Java's graphical interface knowledge
* Window JFrame
* Panel JPanel
* Display the text message label JLabel
* Text Box JTextField
* Button JButton

Threading thread is also involved

Look first:

But leave a question here? is to remove the duplicate numbers (you can do it first, later I will upload)

below to see the code, the code has comments, do not know the message:

Package Thread.test1;import Java.awt.borderlayout;import Java.awt.event.actionevent;import   Java.awt.event.actionlistener;import javax.swing.*;/** * This program simulates a lottery program that randomly extracts 7 groups of numbers from 1-32 * requires Java graphical interface knowledge * Window JFrame * Panel JPanel * Display text message label JLabel * text Box JTextField * Button JButton * * */public class Goodluck_1 extends JFrame {JPanel pn1=null ; JPanel PN2; JPanel PN3; JTextField Tf=null; JLabel Msg=null; JButton btn1; JButton Btn2;boolean flag=true;//is used to determine whether to continue to export the prize number of the logo. In the constructor, initialize the interface public goodluck_1 () {tf=new JTextField (40);//The parameter means how many characters the text box can be placed pn1=new JPanel ();//component placed on panel pn1.add (TF) ;//The panel is placed on the window, the current Window object is this, placed on the top (north) This.add (PN1, Borderlayout.north);/* Java JFrame object, the silent layout manager is "Boundary layout" The class name is BorderLayout * * */msg=new JLabel ("Welcome to the Good Luck lottery program! ");p n2=new JPanel ();p n2.add (msg);//panel placed on the form This.add (PN2, borderlayout.center);//Bottom button section btn1=new JButton (" start ");// Add event Btn1.addactionlistener to Start button (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO Auto-generated Method stub//gives the prize mark assignment/*flag=true;choujiaNg (); *//* * Must use multithreading technology, restart a new thread, go to assign flag * and then call Choujiang (), can be a program recovery response. * */class ThreadStart extends Thread {@Overridepublic void Run () {//TODO auto-generated method stub//here to call the lottery methods flag=true; Choujiang ();}} You must instantiate the above thread inner class, and then start new ThreadStart (). Start ();}); Btn2=new JButton ("Stop");//Add Event Listener to stop button Btn2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method stub//must start a new stop thread in order to terminate the Infinite loop class Threadstop extends thread{@Overridepublic void Run () {//TODO auto-generated method stubflag=false;}} Start the above internal class thread new Threadstop (). Start ();}); Pn3=new JPanel ();p n3.add (BTN1);p n3.add (BTN2); This.add (PN3, Borderlayout.south);//Set the properties of the window This.settitle (" Good luck Winning program 1.0 "); This.setsize, this.setlocation (+);//jframe default Close button, just hide window, You need to override the design shutdown feature this.setdefaultcloseoperation (jframe.exit_on_close); this.setvisible (true);//choujiang ();} For a complex task, the first thing to think about is the encapsulation method public void Choujiang () {/* * * Due to the Start button startup, when it stops, is indeterminate, so the number of cycles is not determined * need to use while loop, and start is infinite loop * */string text= "";  The function is to display the number sequence in the text box while (flag) {/** * Each loop is from 1-32, randomly extract 7 groups of numbers, and spell the string * displayed in the text box * */text= ""; Each time the number is displayed, the newly drawn number for (int i=0;i<7;i++) {int m= (int) (Math.random () *32+1) is displayed again, and if (m<10) {text=text+ "0" +M;} else {text=text+ "" +M;}} It takes a little time to pause, otherwise the loop is too fast, there will be a problem try {thread.sleep),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Displayed in the text box Tf.settext (text);}} public static void Main (string[] args) {//In ongoing development, the less code in the main method, the better. New Goodluck_1 ();}}

There is a need for yourself that past try, play a play!

Java simulates a lottery program

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.