import javax.swing.*;
import java.awt.event.*;
class JFileChooser_Test extends JFrame implements ActionListener{
   JButton jButton1 = new JButton("打开文件");
   JButton jButton2 = new JButton("保存文件");
   JFileChooser jFileChooser1 = new JFileChooser();
   JLabel jLabel1 = new JLabel("你选择的文件名为");
   JScrollPane jScrollPane1 = new JScrollPane();
   JTextArea jTextArea1 = new JTextArea();
   public JFileChooser_Test(){
      setLayout(null);
      setTitle("文件对话框应用");
      jButton1.setBounds(50, 20, 120, 41);
      jButton2.setBounds(50, 70, 120, 41);
      jLabel1.setBounds(50, 120, 140,40);
      jScrollPane1.setBounds(10, 170, 200, 80);
      jFileChooser1.setBounds(1910, 316, 1102, 1010);
      jScrollPane1.getViewport().add(jTextArea1);
      add(jButton1);
      add(jButton2);
      add(jFileChooser1);
      add(jLabel1);
      add(jScrollPane1);
      jButton1.addActionListener(this);
      jButton2.addActionListener(this);
      setBounds(200, 200, 225, 300);
      validate();
      setVisible(true);
   }    
   public void actionPerformed(ActionEvent e){
      if(e.getSource()==jButton1){
          int n = jFileChooser1.showOpenDialog(this);
          String filename = jFileChooser1.getSelectedFile().toString();
          if (n == JFileChooser.APPROVE_OPTION){
           jTextArea1.append(filename);
          }
      }
      else{
          int n = jFileChooser1.showSaveDialog(this);
          String filename = jFileChooser1.getSelectedFile().toString();
          if (n == JFileChooser.APPROVE_OPTION){
              jTextArea1.append(filename);
          }
      }         
    }
}
class JFileChooser_Test{
    public static void main(String args[]){
        JFileChooser_Test ta = new JFileChooser_Test();
    }
}
