Course Training the next day, I on the first day of the base to import folders, the first day at that time has been modified is not, the Internet to find relevant knowledge, asked the students how to import display pictures.
Public class Weiwei 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 documents int index = 0; The ordinal of the current file is int i = 0; JLabel jlbImg1 = null; JLabel jlbImg2 = null; JLabel JLBIMG3 = null; Private Graphics G; /** * 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; }}} public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () { public void Run () {try {Weiwei frame = new Weiwei (); Frame.setvisible (TRUE); } catch (Exception e) {e.printstacktrace (); } } }); }/** * Create the frame. */Public Weiwei () {Settitle ("\u731c\u731c\u770b\u6e38\u620fv0.1"); Setdefaultcloseoperation (Jframe.exit_on_close); SetBounds (100, 100, 645, 500); 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 ()) {//reference: Java file.listfiles (filefilter) filefilter Use//http://zhouzaibao.iteye.com/blog/347557; Get all the files under the folder Filearray = File.listfiles (); num_img = Filearray.length; } } }); Btndir.setbounds (26, 26, 93, 23); Contentpane.add (Btndir);
After learning from the classmate's import method, in their own modified code can click to select the file.
You can click on the selection of the directory has reacted really happy, can go further to modify the code. Import the downloaded picture, you can realize the function of guessing to display the picture.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The second day of Java programming--"guess" game