Java Programming Week 16th Thursday: Use of GUI Programming and file dialog boxes

Source: Internet
Author: User

Part I: Experimental projectsProject One: Documents and Options
Objective: To understand the use of file dialogs and to familiarize yourself with the core steps of event-driven in GUI.
1. Read the blog post "jfilechooser (File Selection dialog box provided by Java Swing) "for the main idea of the blog. 2. Enter (not copy, paste) blog post code in the IDE, Debug and run. 3.in the above interface, add the following controls:JLabel, display text as "Selected file or folder:";JTextField, when the file or folder is selected, the selected file or folder is displayed. 4. Complete the above functions.
Note: 1. Master the common layout of the GUI, the above program, set the layout for FlowLayout, or borderlayout (reference material p138-144). 2. Learn how to view the usage of a Java control and class online, and compare the differences between Chinese and English documents.    Where site:xxx represents a search at the specified URL. Chinese:search in Baidu "site:apihome.cn Java API XXX ", such as"site:apihome.cn Java APIJTextField"English: Search in Baidu "site:docs.oracle.comJava API XXX ", such as"site:docs.oracle.comJava APIJTextField"3. Summarize how Java event-driven works, refer to and rewrite the textbook example 6-1 (p115-116), and rewrite the applet to JFrame.
4. Why do I need to "knock" the code? See blog to all BBS students:What is the best way to learn? "Stupid method of Python", stupid method is more simple.
Package Com.liang;import Java.awt.flowlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.io.file;import Javax.swing.jbutton;import Javax.swing.JFileChooser; Import Javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.jtextfield;public class FileChooser extends JFrame implements Actionlistener{jbutton Open=null; JTextField Jtfpath = null;public static void Main (string[] args) {new Filechooser ();} Public Filechooser () {this.setlayout (New FlowLayout ());//JLabel picture = new JLabel ("Pictureurl");//Add (picture);// Button initialization open=new JButton ("open");//Add monitor open.addactionlistener (this);//Add button to JFrame container this.add (open);//     Add text box control Jtfpath = new JTextField ("Selected file", "Max"); jtfpath.seteditable (false);    Non-editable jtfpath.sethorizontalalignment (jtextfield.center); Center This.add (jtfpath);//Set the size of the JFrame, can be displayed, the default Close button This.setbounds (at (); this.setvisible (true); This.setdefaultcloseoperation (jframe.exit_on_close);} @Overridepublic void ActionperformeD (ActionEvent e) {//TODO auto-generated method Stubjfilechooser jfc=new jfilechooser (); Jfc.setfileselectionmode ( jfilechooser.files_and_directories); 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 Jtfpath.settext (File.getabsolutepath ());}}

Java Programming Week 16th Thursday: Use of GUI Programming and file dialog boxes

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.