Guess number Game

Source: Internet
Author: User

Function: Generate a random number, guess the size of the random number, the opportunity only 3 times, and if wrong, the focus automatically return.
Interface used: ActionListener Focuslistener;

Code:

 PackageCom.niit.guessgame;ImportJava.awt.Color;Importjava.awt.FlowLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.awt.event.FocusEvent;ImportJava.awt.event.FocusListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;ImportJava.util.Random;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JOptionPane;ImportJavax.swing.JTextField;/** *  *//** * @authorAnnie * @date June 6, 2016 * @description: Guess the number game (use the Focus event, activity event)*/ Public classGuessgameextendsJFrameImplementsactionlistener,focuslistener{ Public Static voidMain (string[] args) {NewGuessgame ();    } JLabel Hintlabel;    JButton Buttongetnumber,buttonenter;    JTextField Inputtext;    Random random; intNumber ; intCount =1; intI =3;  PublicGuessgame () {Super("Guess number"); SetLayout (NewFlowLayout ()); SetSize (300, 300); Buttongetnumber=NewJButton ("Get a random number:"); Hintlabel=NewJLabel ("Enter the number you guessed", Jlabel.center);        Hintlabel.setbackground (Color.cyan); Inputtext=NewJTextField (10); Buttonenter=NewJButton ("OK");        Add (Buttongetnumber);        Add (Hintlabel);        Add (Inputtext);        Add (Buttonenter); SetVisible (true); Buttongetnumber.addactionlistener ( This); Buttonenter.addactionlistener ( This); Inputtext.addfocuslistener ( This);        Inputtext.requestfocusinwindow (); Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e);            Dispose ();    }        }); } @Override Public voidactionperformed (ActionEvent e) {if(E.getsource () = =Buttongetnumber) {Random=NewRandom (); number= Random.nextint (1000); Hintlabel.settext ("Please enter the number you want to guess:"); }Else if(E.getsource () = =buttonenter) {            intGuess =integer.valueof (Inputtext.gettext ()); if(Number = =guess) {Hintlabel.settext ("Guess right!" "); }Else if(Number >guess) {Count++; I--; Hintlabel.settext ("Guess small, you still have:" +i+ "chance, please continue to enter");            Inputtext.requestfocusinwindow (); }Else if(Number <guess) {Count++; I--; Hintlabel.settext ("Guess big, you still have:" +i+ "chance, please continue to enter"); Inputtext.requestfocusinwindow ();//Get Focus            }        }        if(Count > 3) {Joptionpane.showconfirmdialog (GetParent (),"Game Over"); }} @Override Public voidfocusgained (focusevent e) {Inputtext=(JTextField) E.getsource (); Inputtext.settext (NULL); } @Override Public voidFocuslost (focusevent e) {//TODO auto-generated Method Stub    }}

Guess number Game

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.