The third day of Java programming--"guess" game

Source: Internet
Author: User
Tags gettext

The basic code can be run through. Show the picture, guess the basic can run, the game overall code has been completed. Is the statistical guess to guess the wrong number of that still in the effort, in the search, I do not know whether can run a guess to guess the wrong number of guesses to see the game.

<pre name= "code" class= "Java" >import java.awt.EventQueue;  Import Javax.imageio.ImageIO;  Import Javax.swing.JFrame;  Import Javax.swing.JPanel;  Import Javax.swing.border.EmptyBorder;  Import Javax.swing.AbstractButton;  Import Javax.swing.ImageIcon;  Import Javax.swing.JButton;  Import Javax.swing.JFileChooser;  Import Javax.swing.JOptionPane;  Import Javax.swing.JTextField;  Import Javax.swing.JLabel;  Import Java.awt.Color;  Import Java.awt.Graphics;  Import Java.awt.Image;  Import Java.awt.SystemColor;  Import Java.awt.event.ActionListener;  Import java.awt.event.ActionEvent;  Import Java.awt.event.MouseAdapter;  Import java.awt.event.MouseEvent;  Import Java.io.File;  Import Java.io.FileFilter;  Import java.io.IOException;    Import Java.util.Random;        Public class Weiwei extends JFrame {/** * */private static final long serialversionuid = 1L;      Private JPanel ContentPane;      Private JTextField Tfdir;      Private JTextField Tfclass; File[] FilearRay folder under All files int num_img = 0; Total number of documents int index = 0;        The ordinal of the current file is int i = 0;      JLabel jlbImg1 = null;      JLabel jlbImg2 = null;        JLabel JLBIMG3 = null;        Private Graphics G;      /** * Launch the application.              */class Myfilefilter implements FileFilter {@Override public boolean accept (File pathname) {              String filename = Pathname.getname (). toLowerCase ();              if (Filename.contains (". jpg")) {return false;              } else {return true;              }}} public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {                      public void Run () {try {Weiwei frame = new Weiwei ();                  Frame.setvisible (TRUE);                  } catch (Exception e) {e.printstacktrace ();      }              }          });   }        /**   * Create the frame.          */Public Weiwei () {Settitle ("\u731c\u731c\u770b\u6e38\u620fv0.1");          Setdefaultcloseoperation (Jframe.exit_on_close);          SetBounds (100, 100, 645, 500);          ContentPane = new JPanel ();          Contentpane.setborder (New Emptyborder (5, 5, 5, 5));          Setcontentpane (ContentPane);            Contentpane.setlayout (NULL);          Select the handler for the directory button JButton btndir = new JButton ("\u9009\u62e9\u76ee\u5f55");                  Btndir.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent arg0) {                  JFileChooser JFC = new JFileChooser ();                  Jfc.setfileselectionmode (jfilechooser.files_and_directories);                  Jfc.showdialog (New JLabel (), "select");                  File File = Jfc.getselectedfile ();                  Tfdir.settext (File.getabsolutepath ()); if (file = null && file.isdirectory ()) {//reference: Java in FILE.LISTFIles (filefilter) filefilter use//http://zhouzaibao.iteye.com/blog/347557;                      Get all the files under the folder Filearray = File.listfiles ();                    num_img = Filearray.length;          }              }          });          Btndir.setbounds (26, 26, 93, 23);            Contentpane.add (Btndir);          text box, display directory Tfdir = new JTextField ();          Tfdir.seteditable (FALSE);          Tfdir.setbounds (125, 27, 450, 21);          Contentpane.add (Tfdir);                    Tfdir.setcolumns (10);          Set Reminder text JLabel J1 = new JLabel ("Guess Who I am, please click my Photo");          J1.setbounds (5, 100, 200, 33);            This.add (J1);          Label, display with guessed student name final JLabel lbguessname = new JLabel ("Student name");          Lbguessname.setbounds (259, 91, 140, 23);            Contentpane.add (Lbguessname);            Label, showing first student photo final JLabel lblImg1 = new JLabel ("Top"); Lblimg1.addmouselistener (New MouseadaPter () {@Override public void mouseclicked (MouseEvent arg0) {if (arg0.getsour                          CE () = = LBLIMG1) {if ((Lblimg1.gettext (). Equals (Lbguessname.gettext ()))) { Joptionpane.showmessagedialog (NULL, "yes! You guessed it!                        "," hint ", joptionpane.plain_message); } else {joptionpane.showmessagedialog (null, "no! You guessed it wrong!                        "," error ", joptionpane.error_message);          }                    }              }          });          Lblimg1.setbounds (26, 155, 150, 200);            Contentpane.add (LBLIMG1);          Label, showing the second student photo final JLabel lblImg2 = new JLabel ("No.2"); Lblimg2.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent arg1 {if (arg1.getsource () = = LblImg2) {if (Lblimg2.gettext (). eqUals (Lbguessname.gettext ()))) {Joptionpane.showmessagedialog (null, "yes! You guessed it!                        "," hint ", joptionpane.plain_message); } else {joptionpane.showmessagedialog (null, "no! You guessed it wrong!                        "," error ", joptionpane.error_message);          }                  }              }          });          Lblimg2.setforeground (Color.Black);          Lblimg2.setbackground (systemcolor.inactivecaption);          Lblimg2.setbounds (241, 155, 150, 200);            Contentpane.add (LBLIMG2);          Label, showing the third student photo final JLabel lblImg3 = new JLabel ("No.3"); Lblimg3.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent ar G2) {if (arg2.getsource () = = LBLIMG3) {if (Lblimg3.gettext (). Equals (lbguessname.g Ettext ()))) {Joptionpane.showmessageDialog (NULL, "yes! You guessed it!                        "," hint ", joptionpane.plain_message); } else {joptionpane.showmessagedialog (null, "no! You guessed it wrong!                        "," error ", joptionpane.error_message);          }                  }                }          });          Lblimg3.setbounds (434, 155, 150, 200);            Contentpane.add (LBLIMG3);            Once again, click on the button to update the corresponding three pictures with a guess student name final JButton btnguessagain = new JButton ("\u518d\u731c\u4e00\u6b21");                  Btnguessagain.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) { if (e.getsource () = = Btnguessagain) {//If the Next button is random random = new Random (system.cu                      Rrenttimemillis ());                      Loop three times ImageIcon icon;                      int k[] = null;                     for (int i = 0; i < 3; i++) {     index = Random.nextint (NUM_IMG);                          String strtmp = filearray[index].tostring ();                          String filename1 = Filearray[index].getname ();                                try {icon = new ImageIcon (Imageio.read (New File (strtmp)));                              Get the picture from the chart image image = Icon.getimage ();                                      Zoom image smallimage = Image.getscaledinstance (150,                                Image.scale_fast);                                Convert image file to ImageIcon icon = new ImageIcon (smallimage);                                if (index = = num_img) index = 0; switch (i) {case 0:system.out.println (filearray[index].get                                  Name ()); K[0]=index;                                 Lblimg1.seticon (icon);                                    Lblimg1.settext (filename1);                              Break                                  Case 1:system.out.println (Filearray[index].getname ());                                  Lblimg2.seticon (icon);                                  Lblimg2.settext (filename1);                                    K[1]=index;                              Break                                  Case 2:system.out.println (Filearray[index].getname ());                                  Lblimg3.seticon (icon);                                  Lblimg3.settext (filename1);                                    K[2]=index;                                Break                              }} catch (IOException E1) {//TODO auto-generated catch block E1.printstackTrace (); }}} @SuppressWarnings ("unused") Random ran                  Dom1 = new Random (index);                    i = index;                  System.out.println (Filearray[index].getname ());                    String filename = Filearray[index].getname ();              Lbguessname.settext (filename);          }          });          Btnguessagain.setbounds (223, 400, 93, 23);      Contentpane.add (Btnguessagain);   }  }


The result of the operation is still possible.


Guess the game code to write, I in 1 reference teachers in the previous log of the relevant code and learn from the network knowledge, coupled with the help of classmates, I can probably run a guess to see the game.

Statistics the number of times there feel very difficult, do not know where to change from the beginning good.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The third day of Java programming--"guess" 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.