• JFileChooser


     http://www.cnblogs.com/dyllove98/archive/2012/03/05/2461895.html

    package swing.choose;
    
    import java.awt.FlowLayout;
    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.JLabel;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class JFileChooseDemo extends JFrame {
        private static final long serialVersionUID = 1L;
    
        private JFileChooser fileChooser = new JFileChooser();;
    
        public JFileChooseDemo() {
    
            JButton button = new JButton("Click");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    int result = fileChooser.showOpenDialog(JFileChooseDemo.this);
                    if (result == JFileChooser.CANCEL_OPTION) {
                        return;
                    }
    
                    File chooseFile = fileChooser.getSelectedFile();
                    add(new JLabel("<html><font color=blue>" + chooseFile.getAbsolutePath()));
                    validate();
                }
            });
            this.add(button);
            this.setLayout(new FlowLayout());
            this.setSize(400, 200);
    
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            JFileChooseDemo demo = new JFileChooseDemo();
            demo.fileChooser.setSelectedFile(new File("test.xls"));
    
            // 设置文件过滤器
    //        demo.fileChooser.setFileFilter(new FileNameExtensionFilter("Description", "gif", "jpg", "bmp"));
            demo.fileChooser.setFileFilter(new FileFilter() {
    
                @Override
                public String getDescription() {
                    return "MS-Excel 2003 文件(.xls)|快捷方式(.lnk)";
                }
    
                @Override
                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    }
                    return f.getName().toLowerCase().endsWith("") || f.getName().toLowerCase().endsWith(".lnk");
                }
            });
    
        }
    }
  • 相关阅读:
    ActiveMQ 5.x 消息队列
    Spring Boot 整合 ElasticSearch 框架
    Spring Boot 整合 Logback 日志框架
    Spring Boot 整合定时任务和异步任务处理
    Spring 中使用 Java 5.0 Executor
    二级指针三种内存模型综合训练
    08-图8 How Long Does It Take (25 分)
    08-图9 关键活动 (30 分)
    08-图7 公路村村通 (30 分)
    C函数之index、strtoul
  • 原文地址:https://www.cnblogs.com/softidea/p/4573388.html
Copyright © 2020-2023  润新知