在Dialog里可以设置对话框是打开,还是保存,我也是初学者,参数不记得,自己查查

解决方案 »

  1.   

    如果你是要图形介面地选择一个文件打开或保存,就用JFileChooser这个类,很好用,以下给出选择一个文件的例子,真接把其存为 filechoolse1.java 文件再编译就可以了!
    图形介面出来后按select 铵钮就会弹出对话框
    import javax.swing.*;
    import java.io.File;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.lang.String;public class filechoolse1{

    public filechoolse1(){





    }



    public static void main(String[] args){
    JFrame frame=new JFrame("main");
       try{

    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    SwingUtilities.updateComponentTreeUI(frame);
    }
    catch (Exception e){
         System.out.print("error");

    }

    frame.setSize(300,200);
    frame.setLocation(100,100);     String[] aa={"chris","amy","f_fire","sarha"};
        JComboBox bb=new JComboBox(aa);
    JButton b=new JButton("select");
    JButton sb=new JButton("combobox ");
    frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
    frame.getContentPane().add(b);
    frame.getContentPane().add(sb);
    frame.getContentPane().add(bb);
    frame.setVisible(true);
            
      



    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){

    JFrame f=new JFrame("choose file");
    JFileChooser c=new JFileChooser();
    c.setDialogTitle("choose file");
    int a=c.showOpenDialog(f);
    File file=c.getSelectedFile();
    String sfilename=file.getName();
    System.out.print(file.getPath());

    } });






    }




    }
      

  2.   

    JFileChooser chooser=new JFileChooser() ;
    chooser.setCurrentDirectory(new File("C:\\")) ; //設置選擇器的默認目錄為C:int result=chooser.showOpenDialog(null) ;
    或者用showSaveDialog()方法 ;