Item One: Random photo display. Objective: To understand the generation of random numbers and the display of images. Target:
(1) The function of displaying three images randomly (or sequentially) is done on the basis of the existing code.
(2) when the guess is accurate, or error, Pop-up dialog box to respond. the effect that needs to be achieved
Code
Import Java.awt.color;import java.awt.eventqueue;import Java.awt.gridlayout;import Java.awt.systemcolor;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.io.file;import Javax.swing.imageicon;import Javax.swing.JButton;import Javax.swing.jfilechooser;import Javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.JPanel;import Javax.swing.jtextfield;import Javax.swing.border.emptyborder;public class Guess01 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 files int index = 0; String strpath = ""; + Folder path String strFileName = ""; + file name JLabel jlbimg = null;/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {Guess01 frame = n EW Guess01 (); Frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the frame. */public Guess01 () {settitle ("\u731c\u731c\u770b\u6e38\u620fv0.1"); Setdefaultcloseoperation (jframe.exit_on_close ); SetBounds (645, 409); contentpane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); contentpane.setlayout (null);//+ Add JLabel control that displays image jlbimg = new JLabel (); Jlbimg.setbackground (color.red), Jlbimg.setbounds (N, A, (), This.add (jlbimg);//Select the Directory button's handler final JButton Btndir = new JButton ("\u9009\u62e9\u76ee\u5f55"); Btndir.addactionlistener (new ActionListener () {Private Object btnguessagain;public void actionperformed (ActionEvent e) {if (E.getsource () ==btndir) {//If the Open button JFileChooser jfc=new JFileChooser ();//jfc.setfileselectionmode (jfilechooser.files_and_directories); Jfc.setfileselectionmode ( jfilechooser.directories_only); Jfc.showdialog (new JLabel (), "select"); File file=jfc.getselectedfile (); if (File.isdirectory ()) {System.ouT.println ("folder:" +file.getabsolutepath ());} else if (File.isfile ()) {System.out.println ("File:" +file.getabsolutepath ());} System.out.println (Jfc.getselectedfile (). GetName ());//The file path is displayed in the text box Tfdir.settext (File.getabsolutepath ());// Jlbimg.seticon (New ImageIcon (File.getabsolutepath ()));//Get file path and file name strpath = File.getabsolutepath (); strFileName = Jfc.getselectedfile (). GetName (); if (File!=null && file.isdirectory ()) {//reference: Java file.listfiles (filefilter ) FileFilter use//http://zhouzaibao.iteye.com/blog/347557;//Get All Files under folder Filearray = File.listfiles (); num_img = Filearray.length;}} if (E.getsource () ==btnguessagain) {//If the Next button is string strtmp = Filearray[index].tostring (); Index++;if (index==num_img ) index = 0;jlbimg.seticon (new ImageIcon (strtmp));} }); Btndir.setbounds (+, +, contentpane.add); (btndir);//Random Image Generation Jlbimg.setlayout (New GridLayout (3, 3)); jlbimg. SetSize (jlbimg) setvisible (true);//jlbimg. Setdefaultcloseoperation (Jlbimg.exit_on_close); jlabel[] labels = new jlabel[100];for (int i = 0; i <; i++) {labels[i] = new JLabel (); jlbimg. Add (Labels[i]); int num = ((int) (Math.random () * 3)); if (num = = 0) {Labels[i].seticon (New ImageIcon ("x.jpg"));} else if (num = = 1) {Labels[i].seticon (New ImageIcon ("0.jpg"));}} text box, display directory Tfdir = new JTextField (); tfdir.seteditable (false) Tfdir.setbounds (363, Contentpane.add) ; Tfdir.setcolumns (10);//Select the handler for the Class button final JButton Btnclass = new JButton ("\u9009\u62e9\u73ed\u7ea7"); Btnclass.addactionlistener (new ActionListener () {private Object btnguessagain;public void actionperformed ( ActionEvent e) {if (E.getsource () ==btnclass) {//If the Open button JFileChooser jfc=new jfilechooser ();// Jfc.setfileselectionmode (jfilechooser.files_and_directories);//jfc.setfileselectionmode ( jfilechooser.directories_only); Jfc.setfileselectionmode (jfilechooser.files_only); File file=jfc.getselectedfile (); Jfc.showdialog (new JLabel (), "select"), if (File.isdirectory ()) {System.out.println ("folder : "+file.getabsolutepath ());} else if (File.isfile ()) {SYstem.out.println ("File:" +file.getabsolutepath ());} System.out.println (Jfc.getselectedfile (). GetName ());//The file path is displayed in the text box Tfclass.settext (File.getabsolutepath ());// Jlbimg.seticon (New ImageIcon (File.getabsolutepath ()));//Get file path and file name strpath = File.getabsolutepath (); strFileName = Jfc.getselectedfile (). GetName ();//if (File!=null && file.istxt ()) {if (File.getname (). EndsWith (". txt")) {// Reference: Use of File.listfiles (FileFilter) FileFilter in Java//http://zhouzaibao.iteye.com/blog/347557;} Get all the files under the folder Filearray = File.listfiles (); num_img = Filearray.length;} if (E.getsource () ==btnguessagain) {//If the Next button is string strtmp = Filearray[index].tostring (); Index++;if (index==num_img ) index = 0;jlbimg. SetIcon (New ImageIcon (strtmp));} }}); Btnclass.setbounds (+, Contentpane.add, btnclass);//text box, display class file Tfclass = new JTextField (); Tfclass.seteditable (false); Tfclass.setbounds (10), Contentpane.add (Tfclass); Tfclass.setcolumns (363);// Label, display with guessed student name JLabel lbguessname = new JLabel ("\u59d3\u540d"); lbGuessname.setbounds (259, 102), Contentpane.add (lbguessname);//label, showing first student photo JLabel lblImg1 = new JLabel ("\u56fe\ u72471 "); Lblimg1.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {}}); Lblimg1.setbounds ( 151, 183, 178); Contentpane.add (LBLIMG1);//label, showing the second student photo JLabel LblImg2 = new JLabel ("\u56fe\u72472"); Lblimg2.addmouselistener (New Mouseadapter () {@Overridepublic void mouseclicked (MouseEvent e) {}}); Lblimg2.setforeground (Color.Black); Lblimg2.setbackground (systemcolor.inactivecaption); LblImg2.setBounds (241, 155, 183, 172); Contentpane.add (LBLIMG2);//label, showing the third student photo JLabel lblImg3 = new JLabel ("\u56fe\u72473"); Lblimg3.addmouselistener (New Mouseadapter () {@Overridepublic void mouseclicked (MouseEvent e) {}}); Lblimg3.setbounds ( 430, 155, 185, 172); Contentpane.add (LBLIMG3);//Guess the button again, click to update the corresponding three pictures with a guess student name JButton btnguessagain = new JButton ("\u518d \u731c\u4e00\u6b21 "); Btnguessagain.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent E{}}); Btnguessagain.setbounds (223, 337, and (); Contentpane.add (Btnguessagain);}}
This effect is only achievedcan't show three pictures yet
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The first day of the 17-week experiment