"Java Programming" 16th Week Thursday: GUI Programming and File dialog box use

Source: Internet
Author: User
Package Qiang;
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{/** * */private static final long serialversion
	UID = 1L;
	/** * * * * * * JButton open=null;
	JTextField Jtfpath=null;
	public static void Main (string[] args) {new Filechooser ();
		
	Public Filechooser () {this.setlayout (New FlowLayout ());
	Open=new JButton ("open");
	Open.addactionlistener (this);
	
	This.add (open);
	JTextField Jtfpath = new JTextField ("Selected file", 40);
	Jtfpath.seteditable (FALSE);
	Jtfpath.sethorizontalalignment (Jtextfield.center);
	
	
	
	This.add (Jtfpath);
	This.setbounds (400,200,100,100);
	This.setvisible (TRUE);
	This.setdefaultcloseoperation (Jframe.exit_on_close);
	Open.addactionlistener (this); } @Override
public void actionperformed (ActionEvent e) {jfilechooser jfc=new jfilechooser ();
	   Jfc.setfileselectionmode (jfilechooser.files_and_directories);
	   Jfc.showdialog (New JLabel (), "selection");
	   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 ()); }
}

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.