JFileChooser class can create file dialog box
There are two ways to save and open the following
Showsavedialog (Component a); Showopendialog (Component a);
Parameter A is the location of the file dialog box, NULL is in the center of the screen, this is in the center of the window
The above method returns two constants when the "OK", "Cancel", "Close" of the User Action dialog box
Jfilechooser.approve_option
Jfilechooser.cancel_option
If you want the file dialog box to manipulate the file type is required, such as "JPG" graphic ah, you can
Filenameextensionfilter filter1=new filenameextensionfilter ("Graphics file", "JPG", "gif")
The Setfilefilter (Filenameextensionfilter filter) method is then invoked with an instance of the file dialog box
Test code
PackageTestwin;ImportJava.awt.EventQueue;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.*;Importjavax.swing.*;ImportJavax.swing.JFrame;ImportJavax.swing.JScrollPane;Importjava.awt.BorderLayout;ImportJavax.swing.JTextField;ImportJavax.swing.filechooser.FileNameExtensionFilter; Public classTestwin { Public Static voidMain (String args[]) {Mywin win1=NewMywin (); }}classMywinextendsJFrameImplementsactionlistener{jfilechooser file1;//file dialog BoxJMenuBar menubar; JMenu menu; JMenuItem Save,open; JTextArea text; BufferedReader in1;//Buffered InputFileReader FileReader;//file character inputBufferedWriter out1;//Buffered OutputFileWriter FileWriter;//file character OutputMywin () {init (); SetBounds (30,30,300,400); SetVisible (true); Setdefaultcloseoperation (Jframe.exit_on_close); } voidinit () {text=NewJTextArea (10,10); Add (NewJScrollPane (text), borderlayout.center); MenuBar=NewJMenuBar (); Menu=NewJMenu ("File"); Save=NewJMenuItem ("Save"); Open=NewJMenuItem ("Open"); Save.addactionlistener ( This); Open.addactionlistener ( This); Menu.add (save); Menu.add (open); Menubar.add (menu); Setjmenubar (menubar); File1=NewJFileChooser (); Filenameextensionfilter Filter=NewFilenameextensionfilter ("Java file", "TXT");//file name FilteringFile1.setfilefilter (filter);//Filter Down } Public voidactionperformed (ActionEvent e) {if(E.getsource () = =Save) { intState=file1.showsavedialog ( This);//Display the Save box if(state==jfilechooser.approve_option) { Try{File dir=file1.getcurrentdirectory ();//Get save PathString name=file1.getselectedfile (). GetName ();//get the name of the choiceFile file=NewFile (dir,name); FileWriter=NewFileWriter (file); OUT1=NewBufferedWriter (FileWriter); Out1.write (Text.gettext ()); Out1.close (); Filewriter.close (); } Catch(IOException exp) {}}} Else if(E.getsource () = =Open) { intState=file1.showopendialog ( This); if(state==jfilechooser.approve_option) { Try{File dir=file1.getcurrentdirectory (); String name=file1.getselectedfile (). GetName (); File File=NewFile (dir,name); FileReader=Newfilereader (file); In1=NewBufferedReader (FileReader); String s=NULL; for(;(s=in1.readline ())! =NULL;) {text.append (s+ "\ n"); } in1.close (); Filereader.close (); } Catch(IOException exp) {}}} }}
File dialog box