Swing implementation File selection (directory selection) with export

Source: Internet
Author: User

Specific build Tools

(1)

(2)

(3)

(4)

Source:

Example.java

[Java]View Plaincopyprint?
  1. Package  Org.qiailin.jframe;
  2. Import  Java.awt.Container;
  3. Import  Java.awt.Point;
  4. Import  Java.awt.Toolkit;
  5. Import  java.awt.event.ActionEvent;
  6. Import  Java.awt.event.ActionListener;
  7. Import  Java.io.File;
  8. Import  Javax.swing.JButton;
  9. Import  Javax.swing.JFileChooser;
  10. Import  Javax.swing.JFrame;
  11. Import  Javax.swing.JLabel;
  12. Import  Javax.swing.JOptionPane;
  13. Import  Javax.swing.JTabbedPane;
  14. Import  Javax.swing.JTextField;
  15. /**
  16. * Because see a lot of friends in the trial temporary time to write a simple case source stacking up, need to see, because the time rush to do rough January 4, 2011 23:22:31
  17.  * 
  18. * @author Paint Arryn QQ 172794299 e-mail [email protected]
  19.  * 
  20.  */   
  21. Public class jexample implements ActionListener {
  22. JFrame frame = new JFrame ("Lacquer Arryn-example"); //Frame layout   
  23. JTabbedPane Tabpane = new JTabbedPane (); //Tab Layout   
  24. Container con = new Container (); //   
  25. JLabel Label1 = new JLabel ("file directory");
  26. JLabel Label2 = new JLabel ("Select File");
  27. JTextField Text1 = new JTextField (); path of the//TextField directory   
  28. JTextField Text2 = new JTextField (); //path to file   
  29. JButton button1 = new JButton ("..."); //Select   
  30. JButton button2 = new JButton ("..."); //Select   
  31. JFileChooser JFC = new jfilechooser (); //File selector   
  32. JButton Button3 = new JButton ("OK"); //   
  33. Jexample () {
  34. Jfc.setcurrentdirectory (new file ("d://"));//The initial directory of the file selector is set to D-Disk
  35. Double  lx = Toolkit.getdefaulttoolkit (). Getscreensize (). GetWidth ();
  36. Double  ly = Toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ();
  37. Frame.setlocation (new point ((int) (LX/ 2)- (int) (ly/ 2)- ()); //Set window where it appears   
  38. Frame.setsize (280, ); //Set window size   
  39. Frame.setcontentpane (Tabpane); //Set layout   
  40. Label1.setbounds (ten , ten , +);
  41. Text1.setbounds ( the Ten , the  );
  42. Button1.setbounds ( all, ten , +);
  43. Label2.setbounds (ten , A.  );
  44. Text2.setbounds ( +--)   );
  45. Button2.setbounds ( a);
  46. Button3.setbounds ( A.  );
  47. Button1.addactionlistener (this); //Add event handling   
  48. Button2.addactionlistener (this); //Add event handling   
  49. Button3.addactionlistener (this); //Add event handling   
  50. Con.add (Label1);
  51. Con.add (Text1);
  52. Con.add (button1);
  53. Con.add (Label2);
  54. Con.add (TEXT2);
  55. Con.add (button2);
  56. Con.add (Button3);
  57. Frame.setvisible (true); //Window visible   
  58. Frame.setdefaultcloseoperation (Jframe.exit_on_close); //Enable to close the window and end the program   
  59. Tabpane.add ("1 Panel", con); //Add layout 1   
  60. }
  61. /**
  62. * Method of time monitoring
  63.      */   
  64. Public void actionperformed (ActionEvent e) {
  65. //TODO auto-generated method stub   
  66. if (E.getsource (). Equals (button1)) { //What is the button that determines the trigger method
  67. Jfc.setfileselectionmode (1); //settings can only be selected to folder   
  68. int state = jfc.showopendialog (null); //This sentence is the trigger statement that opens the file selector interface   
  69. if (state = = 1) {
  70. return  ;
  71. } Else {
  72. File f = jfc.getselectedfile (); //F for the selected directory   
  73. Text1.settext (F.getabsolutepath ());
  74. }
  75. }
  76. //Bind to selection file, select File Event   
  77. if (E.getsource (). Equals (Button2)) {
  78. Jfc.setfileselectionmode (0); //settings can only be selected to file   
  79. int state = jfc.showopendialog (null); //This sentence is the trigger statement that opens the file selector interface   
  80. if (state = = 1) {
  81. return ; //Revoke return   
  82. } Else {
  83. File f = jfc.getselectedfile (); //F for the selected file   
  84. Text2.settext (F.getabsolutepath ());
  85. }
  86. }
  87. if (E.getsource (). Equals (Button3)) {
  88. //Pop-up dialog can change the parameters inside the concrete depends on everyone to see, the time is very short   
  89. Joptionpane.showmessagedialog (null, "Popup Instance of the dialog box, welcome you--Paint Irene!  ", " hint ", 2);
  90. }
  91. }
  92. Public static void main (string[] args) {
  93. New  jexample ();
  94. }
  95. }

This step takes note of the class that selects the Main method

Simple export is OK, packaging can be used Ant and Maven, of course, here does not introduce interested can go to see

Interested can go to download

Of course, if you have friends who want to get your jar files out of Java, you can generate EXE files where you can use exe4j

Of course here exe4j believe that everyone will use it will not use the words can go to Google.

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.