Java Course Internship Journal (Thursday) guess the annotated version

Source: Internet
Author: User

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.JTextField; Import Javax.swing.JLabel; Import Java.awt.Color; 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; +import Javax.swing.JOptionPane; +public class Guess3 extends JFrame {/** * */private static final long Serialversionuid = 1l;private JPanel contentp Ane;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;    String strpath = ""; + Folder path string strFileName = ""; + file name JLabel jlbimg = null; JLabel jlbImg1 = null; JLabel jlbImg2 = null; JLabel JLBIMG3 = null;/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {Guess3 frame = NE W GUESS3 (); frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the frame. */public Guess3 () {settitle ("Guess Tyhban"),///upper-left title setdefaultcloseoperation (jframe.exit_on_close);//close interface, if not write this sentence, The interface is gone, but the program also resides in memory without ending setbounds (645, 510);//setbounds (x,y,width,height); X: The beginning of the component on the x-axis of the container y: The beginning of the component on the y-axis of the container width: the length of the component height: ContentPane = new JPanel ();//panel Contentpane.setborder (new Emptyborder (5, 5, 5, 5));//Set the boundary of the panel, border describes the boundary around the panel (belonging to the inside of the panel), Emptyborder is a blank boundary;//The statement means that the contentpane inner border is empty, And with a thickness of 5 pixels, if you add a button (set to fill) directly above the ContentPane, the button will be filled with an inner rectangle Setcontentpane (ContentPane) other than the border; contentpane.setlayout (NULL)///Interface layout//Select Directory button handler 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"); Directory select File file=jfc.getselectedfile (); Tfdir.settext (File.getabsolutepath ()); if (File!=null && file.isdirectory ()) {//Get all the files under the folder, Show all files for this folder Filearray = File.listfiles (); num_img = Filearray.length;}}); Btndir.setbounds (26, 26, 93, 23);//Select directory One, SetBounds (x,y,width,height) Contentpane.add (btndir);//Select directory 1//text box, Display directory Tfdir = new JTextField ();//Build a text box, the text box can enter the character is infinite tfdir.seteditable (false);//Set not editable Tfdir.setbounds (125, 27, 450, 21 );//setbounds (x,y,width,height) Contentpane.add (tfdir);//text Box//tfdir.setcolumns (10);//Select the class button handler JButton btnclass = new JButton ("\u9009\u62e9\u73ed\u7ea7"); Btnclass.addactionlistener (new ActionListener () {public void  actionperformed (ActionEvent e) {jfilechooser jfc=new jfilechooser ();  Jfc.setfileselectionmode (jfilechooser.files_and_directories); jfc.shOwdialog (New JLabel (), "select"); File file=jfc.getselectedfile (); Tfclass.settext (File.getabsolutepath ());}}); Btnclass.setbounds, Contentpane.add (Btnclass);//Select Directory Two//text box, display class file Tfclass = new JTextField (); Tfclass.seteditable (false); Tfclass.setbounds ((), Contentpane.add (Tfclass);//text Box Tfclass.setcolumns ( 10);//label, display with guessed student name final JLabel lbguessname = new JLabel ("name"); Lbguessname.setbounds (259, 91,140, 23°c); Contentpane.add ( Lbguessname);//name display//tag, show First student photo final JLabel lblImg1 = new JLabel ("Picture One"); Lblimg1.addmouselistener (New Mouseadapter ( {@Overridepublic void mouseclicked (MouseEvent arg0) {if (Arg0.getsource () ==lblimg1) {//+if (Lblimg1.gettext (). Equals (Lbguessname.gettext ()))) {Joptionpane.showmessagedialog (null, "Wow, you guessed right", "hint", joptionpane.plain_message);}  else {Joptionpane.showmessagedialog (null, "No, guessed wrong", "error", Joptionpane.error_message);} }}); Lblimg1.setbounds (26,155, Contentpane.add); (lblImg1);//Picture one shows JLBIMG1 = new JLabel (); Jlbimg1.setbackground (color.red); JLBimg1.setbounds (151, 181,201);//jlabel in Javax.swing label in Java.awtthis.add (JLBIMG1);//label, show second student photo final JLabel LblImg2 = new JLabel ("Picture Two"); Lblimg2.addmouselistener (new Mouseadapter () {@Overridepublic void mouseclicked (MouseEvent ARG1) {if (Arg1.getsource () ==lblimg2) {//+if ((Lblimg2.gettext (). Equals (Lbguessname.gettext ()))) { Joptionpane.showmessagedialog (NULL, "Wow, you guessed right", "hint", joptionpane.plain_message);}  else {Joptionpane.showmessagedialog (null, "No, guessed wrong", "error", Joptionpane.error_message);} }}); Lblimg2.setforeground (Color.Black); Lblimg2.setbackground (systemcolor.inactivecaption); LblImg2.setBounds ( 241,155, Contentpane.add (LBLIMG2);//Picture two shows JlbImg2 = new JLabel (); Jlbimg2.setbackground (color.red); Jlbimg2.setbounds (155, 183, 201); This.add (JLBIMG2);//label, showing third student photo final JLabel lblImg3 = new JLabel ("Picture three"); Lblimg3.addmouselistener (New Mouseadapter () {@Overridepublic void mouseclicked (MouseEvent arg2) {if (Arg2.getsource () ==LBLIMG3) {//+if (Lblimg3.gettext () Equals (Lbguessname.gettext ()) {Joptionpane.showmessagedialog (null, "Wow, you guessed right", "hint", joptionpane.plain_message);}  else {Joptionpane.showmessagedialog (null, "No, guessed wrong", "error", Joptionpane.error_message);} }}); Lblimg3.setbounds (434,155, contentpane.add); jlbImg3 = new JLabel (); Jlbimg3.setbackground ( color.red); Jlbimg3.setbounds (434, 155, 185, 201); This.add (JLBIMG3);//Guess the button again, click 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.currenttimemillis ());//random number ImageIcon icon;for (int i=0;i<3;i++) {index = Random.nextint (NUM_IMG);// Random number call String strtmp = Filearray[index].tostring ();//Picture array string filename1=filearray[index].getname ();//array of names try {icon = New ImageIcon (Imageio.read (strtmp));//Gets the picture from the chart image image = Icon.getimage (); Zoom image smallimage = Image.getscaledinstance (150,200,image.scale_fast);//Convert image file to Imageiconicon = new ImageIcon (smallimage); if (index==num_img) index = 0;switch (i) {case 0: System.out.println (Filearray[index].getname ()); Lblimg1.seticon (icon); Lblimg1.settext (filename1); Break;case 1:  System.out.println (Filearray[index].getname ());  Lblimg2.seticon (icon); Lblimg2.settext (filename1); Break;case 2:system.out.println (Filearray[index].getname ()); Lblimg3.seticon (icon); Lblimg3.settext (filename1); break;}} catch (IOException E1) {e1.printstacktrace ();}}} @SuppressWarnings ("unused") random random1 = new Random (index); System.out.println (Filearray[index].getname ());//Output Picture name}});//==+btnguessagain.setbounds (223, 400, 93, 23);//  SetBounds (x,y,width,height) Contentpane.add (Btnguessagain);//next}}

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

Java Course Internship Journal (Thursday) guess the annotated version

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.