Java Programming Week 16th Thursday: GUI Programming and File dialog using task two

Source: Internet
Author: User

Item Two: Picture DisplayObjective: To understand the common way of GUI image display. Goal: Use project one to get the image file that needs to be displayed, and display it on the interface (see: What controls are used in the Java swing to load a picture?), Java programming 14th Week of experiment: GUI programming Preliminary)
Package Com.liang;import Java.awt.color;import Java.awt.flowlayout;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.io.file;import Java.io.filefilter;import Javax.swing.ImageIcon;import Javax.swing.jbutton;import Javax.swing.jfilechooser;import Javax.swing.jframe;import Javax.swing.JLabel;import Javax.swing.jtextfield;class Myfilefilter implements filefilter{@Override public boolean accept (File pathn          AME) {String filename = pathname.getname (). toLowerCase ();          if (Filename.contains (". jpg")) {return false;          }else{return true; }}} public class Filechooser extends JFrame implements actionlistener{/** * */private static fin        Al long serialversionuid = 1L;    JButton Open=null;        JTextField Jtfpath = null;    JLabel jlbimg = null;        JButton btnnext = null;    String strpath = "";    Folder path String strFileName = ""; File name file[] FileARray;    folder under All files int num_img = 0;    Total number of documents int index = 0;    The current file's ordinal public static void main (string[] args) {new Filechooser ();        } public Filechooser () {this.settitle ("Week16");                Set layout mode this.setlayout (new FlowLayout ());        Button initialization open=new JButton ("select Directory");        Add monitoring Open.addactionlistener (this);                Add the button to the JFrame container this.add (open);        Add text box control Jtfpath = new JTextField ("Selected file", 40);     Jtfpath.seteditable (FALSE);    Non-editable jtfpath.sethorizontalalignment (jtextfield.center);                Center This.add (Jtfpath);        Show Next Picture btnnext = new JButton ("Show Next");        This.add (Btnnext);                    Btnnext.addactionlistener (this);        Add the JLabel control that displays the image jlbimg = new JLabel ();        Jlbimg.setbackground (color.red);        Jlbimg.setbounds (100, 100, 200, 200);                This.add (JLBIMG); Set the size of the jframe, which can be displayed, by default off byButton This.setbounds (400, 200, 700, 500);        This.setvisible (TRUE);            This.setdefaultcloseoperation (Jframe.exit_on_close); } @Override public void actionperformed (ActionEvent e) {//TODO auto-generated method Stub// When multiple components need to be monitored, the event response is encoded in the IF (E.getsource () ==open) {//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 ()); Display the file path in the text box Jtfpath.settext (File.getabsolutepath ());                        Jlbimg.seticon (New ImageIcon (File.getabsolutepath ()));            Get file path with file name strpath = File.getabsolutepath ();                        strFileName = Jfc.getselectedfile (). GetName ();                 if (File!=null && file.isdirectory ()) {//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 () ==btnnext) {//If the Next button is String strtmp = Filearray[index].            ToString ();            index++;            if (index==num_img) index = 0;        Jlbimg.seticon (New ImageIcon (strtmp));   }           }}






Part II: Job Blog Requirements1. In the job blog, complete at least 2 of the above 3 projects, and write the results of the operation, the code to the blog.
2. In the job blog, answer the following three questions:
(1) Describe the event-driven mode of GUI in Java. event handling mode, various event listeners ....

(2) Briefly describe the common ways in which strings are converted to numeric values and converted into strings in Java. The string type is converted to various numeric types: string s = "169";  byte B = byte.parsebyte (s);  Short T = Short.parseshort (s);  int i = Integer.parseint (s);  Long L = Long.parselong (s);  Float f = float.parsefloat (s); Double d = double.parsedouble (s);

a numeric type is converted to a string: French one: string s = string.valueof (value);//Where value is of any number type. Law two: String AA = 1+ "";


(3) Briefly in Java,the core code used by JFileChooser.
import Java.io.File;  Import Javax.swing.JFileChooser; Import Javax.swing.filechooser.FileFilter; public class Filechoosertest {public static void main (String [] args) {

Java Programming Week 16th Thursday: GUI Programming and File dialog using task two

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.