Package Xuexi;
Import Java.awt.EventQueue;
Import Javax.imageio.ImageIO;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.border.EmptyBorder;
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; @SuppressWarnings ("unused") public class Guess01 extends JFrame {/** * */private static final long SE
Rialversionuid = 1L;
Private JPanel ContentPane;
Private JTextField Tfdir;
Private JTextField Tfclass; File[] Filearray; All files under folder 1 int num_img = 0; Total Documents
int index = 0;
The serial number of the current file is private int idimg = 1;
JLabel jlbImg1 = null;
JLabel jlbImg2 = null;
JLabel JLBIMG3 = null;
/** * 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; }}//2 public static void Main (string[] A
RGS) {Eventqueue.invokelater (new Runnable () {public void run () {try {
Guess01 frame = new 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 (100, 100, 700, 530);
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) {
3 JFileChooser jfc=new jfilechooser ();
Jfc.setfileselectionmode (jfilechooser.files_and_directories);
Jfc.showdialog (New JLabel (), "selection");
File File=jfc.getselectedfile ();
Tfdir.settext (File.getabsolutepath ()); if (File!=null && file.isdirectory ()) {
4//Get all files under the folder Filearray = File.listfiles ();
num_img = Filearray.length;
}
}
});
Btndir.setbounds (26, 26, 93, 23);
Contentpane.add (Btndir);
text box, showing directory tfdir = new JTextField ();
Tfdir.seteditable (FALSE);
Tfdir.setbounds (125, 27, 363, 21);
Contentpane.add (Tfdir);
Tfdir.setcolumns (10);
Select the handler for the class button JButton Btnclass = new JButton ("\u9009\u62e9\u73ed\u7ea7");
Btnclass.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {
5 JFileChooser jfc=new JFileChooser (); Jfc.setfileselectionmode (Jfilechooser.files_and_directoRIES);
Jfc.showdialog (New JLabel (), "selection");
File File=jfc.getselectedfile ();
Tfclass.settext (File.getabsolutepath ());
6}});
Btnclass.setbounds (26, 59, 93, 23);
Contentpane.add (Btnclass);
text box, showing class file Tfclass = new JTextField ();
Tfclass.seteditable (FALSE);
Tfclass.setbounds (125, 60, 363, 21);
Contentpane.add (Tfclass);
Tfclass.setcolumns (10);
Label, show with guess student name JLabel lbguessname = new JLabel ("\u59d3\u540d");
Lbguessname.setbounds (259, 91, 102, 23);
Contentpane.add (Lbguessname);
Label, showing the first student photo JLabel lblImg1 = new JLabel ("\u56fe\u72471"); Lblimg1.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent arg0)
{
}
}); Lblimg1.setbounds (105, 290, 183, 178); Contentpane.add (LBLIMG1);
7 JLBIMG1 = new JLabel ();
Jlbimg1.setbackground (color.red);
Jlbimg1.setbounds (26, 155, 185,201);
This.add (JLBIMG1);
8//Label, display second student photo JLabel LblImg2 = new JLabel ("\u56fe\u72472");
Lblimg2.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent e) {
}
});
Lblimg2.setforeground (Color.Black);
Lblimg2.setbackground (systemcolor.inactivecaption);
Lblimg2.setbounds (330, 290, 183, 172); Contentpane.add (LBLIMG2);
9 JlbImg2 = new JLabel ();
Jlbimg2.setbackground (color.red);
Jlbimg2.setbounds (241, 155, 185, 201); This.add (JLBIMG2); 10//Label, displayShows the third student photo JLabel lblImg3 = new JLabel ("\u56fe\u72473");
Lblimg3.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent e) {
}
});
Lblimg3.setbounds (540, 290, 183, 172);
Contentpane.add (LBLIMG3); JLBIMG3 = new JLabel ();
One jlbimg3.setbackground (color.red);
Jlbimg3.setbounds (456, 155, 185, 201); This.add (JLBIMG3); 12//Guess again button, click then update the corresponding three pictures with a guess student name final JButton btnguessagain = new JButton ("\u518d\u731c\u4
E00\u6b21 ");
Btnguessagain.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) { if (E.getsource () ==btnguessagain) {//If Next
Button String strtmp = filearray[index].tostring (); index++;
if (index==num_img) index = 0;
Jlbimg1.seticon (New ImageIcon (strtmp));
index++;
Jlbimg2.seticon (New ImageIcon (strtmp));
index++;
if (E.getsource () ==btnguessagain) {//If the Next button String STRTMP2 = filearray[index].tostring ();
index++;
Jlbimg2.seticon (New ImageIcon (STRTMP2));
index++;
} String STRTMP1 = Filearray[index].tostring ();
index++;
Jlbimg3.seticon (New ImageIcon (STRTMP1));
}//14}); Btnguessagain.setbounds (250, 430, 93, 23);
Contentpane.add (Btnguessagain);
}
}
Summary: Today, the teacher can finally display the code on the picture. Set three jlbimg blank variables so that three photos are inserted into JLabel with JLBLMG. Insufficient is, how also can not get the random display picture, puzzled me, very embarrassed. I believe that one day I can do it, just want me to struggle.