Java Course Internship Journal (Thursday)

Source: Internet
Author: User
Tags gettext

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 ("\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 folder 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 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);//text box, display class file Tfclass = new JTextField (); Tfclass.seteditable (false); Tfclass.setbounds (10), Contentpane.add (tfclass); tfclass.setcolumns; Label, display with guessed student name final JLabel lbguessname = new JLabel ("name");Lbguessname.setbounds (259, 91,140, 23°c); Contentpane.add (lbguessname);//label, showing 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, 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 it. "," 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 ());//+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;}



Complete the Tuesday and Wednesday code combination, other functions will not be done, can only be achieved here, if you want to make the picture and the name can be added to the words of the array

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

Java Course Internship Journal (Thursday)

Related Article

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.