JfileChooser

package com.zzst.application.meeting.dwr.control;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Insets;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class A {
 static private final String newline = "\n";
     JButton openButton, saveButton;
     JTextArea log;
     JFileChooser fc;
     JFrame jf = null;
    
     public A(){
     jf = new JFrame();
     Container cp = jf.getContentPane();
     log = new JTextArea(5,20);
         log.setMargin(new Insets(5,5,5,5));
         log.setEditable(false);
         JScrollPane logScrollPane = new JScrollPane(log);
         fc = new JFileChooser();
         openButton = new JButton("Open a File...");
         openButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e) {
 int returnVal = fc.showOpenDialog(jf);
             if (returnVal == JFileChooser.APPROVE_OPTION) {
                 File file = fc.getSelectedFile();              
                 log.append("Opening: " + file.getName() + "." + newline);
             } else {
                 log.append("Open command cancelled by user." + newline);
             } }
         });
         saveButton = new JButton("Save a File...");
         saveButton.addActionListener(new ActionListener(){

 public void actionPerformed(ActionEvent e) {
 int returnVal = fc.showSaveDialog(jf);
             if (returnVal == JFileChooser.APPROVE_OPTION) {
                 File file = fc.getSelectedFile();
                
                 log.append("Saving: " + file.getName() + "." + newline);
             } else {
                 log.append("Save command cancelled by user." + newline);
             }

 }});
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(openButton);
         buttonPanel.add(saveButton);
         cp.add(buttonPanel, BorderLayout.PAGE_START);
         cp.add(logScrollPane, BorderLayout.CENTER);
         jf.setVisible(true);
         jf.pack();
         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }
     public static void main(String []args){
     new A();
     }
 }

你可能感兴趣的:(java,swing,DWR)