日期:2014-05-20 浏览次数:20745 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
public class FileChooserExample {
public static void main(final java.lang.String[] args) {
java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
@Override public void run(){
final JFrame frame = new JFrame("FileChooser Example");
final DefaultListModel<File> model = new DefaultListModel<>();
final JList<File> list = new JList<>(model);
final JToolBar toolbar = new JToolBar();
final JFileChooser filechooser = new JFileChooser();
filechooser.setMultiSelectionEnabled(true);
filechooser.setFileFilter(new FileNameExtensionFilter("JPEG & GIF & PNG Images", "jpg", "jpeg", "gif", "png"));
Action select = new AbstractAction("Select ..."){
@Override public void actionPerformed(ActionEvent e) {
if(JFileChooser.APPROVE_OPTION == filechooser.showOpenDialog(frame)){
for(File file: filechooser.getSelectedFiles())
model.addElement(file);
}