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; All files under directory int num_img = 0; Total number of documents int index = 0; The ordinal of the current file is int i = 0; String strpath = ""; + Directory path StrinG strFileName = ""; + filename 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 ("\u731c\u731c\u770b\u6e38\u620fv0.1"); Setdefaultcloseoperation (Jframe.exit_on_close) ; SetBounds (645, 510); 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 ()) {//Get all Files under directory Filearray = File.listfiles (); num_img = Filearray.length;}}); Btndir.setbounds (Contentpane.add), Btndir,//text box, display directory Tfdir = new JTextField (); Tfdir.seteditable (false) Tfdir.setbounds, Contentpane.add (Tfdir); Tfdir.setcolumns (10);//Select the handler for class button 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);//text box, display class file Tfclass = new JTextField (); Tfclass.seteditable (false); Tfclass.setbounds (10), Contentpane.add (tfclass); tfclass.setcolumns; Label, display with student name final JLabel lbguessname = new JLabel ("surname(259, 91,140, lbguessname.setbounds); Contentpane.add (lbguessname);//tags. 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); jlbImg1 = new JLabel (); Jlbimg1.setbackground ( color.red); Jlbimg1.setbounds (151, 181,201); This.add (JLBIMG1);//label, showing 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); jlbImg2 = new JLabel (); Jlbimg2.setbackground (color.red); Jlbimg2.setbounds (155, 183, 201); This.add (JLBIMG2);//label. Show 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 the student name final JButton Btnguessagain = new JButton ("\u518d\u731c\u4e00\u6b21 ") Btnguessagain.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {if ( E.getsource () ==btnguessagain) {//assumed to be nextbuttonrandom random = new Random (System.currenttimemillis ());//+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,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);//i = index; System.out.println (Filearray[index].getname ()); String filename=filearray[index].getname (); lbguessname.settext (filename);}}); /==+btnguessagain.setbounds (223, +, contentpane.add); btnguessagain;}
After the combination of the Tuesday and Wednesday code, the other functions will not be done, just to achieve here. Suppose you want to add an array to the image and name.
Java Course Internship Journal (Thursday)