我用SWING编程时想要定制一个打开对话框,对话框只能打开BMP和JPG文件,而且首先显示的是程序所在文件夹下的文件,该怎么写?

解决方案 »

  1.   

    你写一个类 然后实现以下FileFilter接口 就可以了
      

  2.   

    直接implements这个接口  然后把它下面的方法按你的要求重写一下 就好了啊  
      

  3.   

    没时间细调了,自己调去吧
    package com.syj;import java.awt.BorderLayout;
    import java.awt.Font;
    import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.filechooser.FileFilter;/**
     * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
     * Builder, which is free for non-commercial use. If Jigloo is being used
     * commercially (ie, by a corporation, company or business for any purpose
     * whatever) then you should purchase a license for each developer using Jigloo.
     * Please visit www.cloudgarden.com for details. Use of Jigloo implies
     * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
     * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
     * ANY CORPORATE OR COMMERCIAL PURPOSE.
     */
    public class NewJFrame extends javax.swing.JFrame {
    private JButton jButton1; /**
     * Auto-generated main method to display this JFrame
     */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    NewJFrame inst = new NewJFrame();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    } public NewJFrame() {
    super();
    initGUI();
    } private void initGUI() {
    try {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    {
    jButton1 = new JButton();
    getContentPane().add(jButton1, BorderLayout.CENTER);
    jButton1.setText("jButton1");
    }
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    chooser.setApproveButtonText("选择");
    chooser.setFont(new Font("宋体", Font.PLAIN, 14));
    chooser
    .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setFileFilter(new FileFilter() {
    public boolean accept(File f) {
    String filePath = f.getAbsoluteFile().toString()
    .toUpperCase();
    if (filePath.endsWith("BMP")
    || filePath.endsWith("JPG"))
    return true;
    else
    return false;
    } public String getDescription() {
    return null;
    }
    });
    int returnVal = chooser.showOpenDialog(getContentPane());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    String temp = String.valueOf(chooser.getSelectedFile());
    System.out.println(temp);
    }
    }
    });
    pack();
    setSize(400, 300);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}
      

  4.   

    先谢谢4#,但我还想问问设置对话框里的文件类型选择呢?即我希望可以在(*.bmp *.jpg),(*.bmp),(*.jpg),(*.*)这四个文件类型选项中选择?
      

  5.   

    没问题了就结贴吧                    chooser.addChoosableFileFilter(new FileFilter() {
                            public boolean accept(File f) {
                                String filePath = f.getAbsoluteFile().toString()
                                        .toUpperCase();
                                if (filePath.endsWith("BMP")
                                        || filePath.endsWith("JPG"))
                                    return true;
                                else
                                    return false;
                            }                        public String getDescription() {
                                return "aaa";
                            }
                        });