zoukankan      html  css  js  c++  java
  • JavaSwing文件选择器 JFileChooser的使用

    先看效果吧!

    说明:选择文件或者文件夹。本例子就直接在控制台输出文件或者文件夹的路径。实际开发中,就可以将文件或文件夹的路径封装为File的实例来使用了。

    package test;
    
    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;
    
    @SuppressWarnings("serial")
    public class FileChooser extends JFrame implements ActionListener{  
        JButton open=null;  
        public static void main(String[] args) {  
            new FileChooser();  
        }  
        public FileChooser(){  
            open=new JButton("open");  
            this.add(open);  
            this.setBounds(400, 200, 100, 100);  
            this.setVisible(true);  
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            open.addActionListener(this);  
        }  
        @Override  
        public void actionPerformed(ActionEvent e) {  
            // TODO Auto-generated method stub  
            JFileChooser jfc=new JFileChooser();  
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  
            jfc.showDialog(new JLabel(), "选择");  
            File file=jfc.getSelectedFile();  
            if(file.isDirectory()){  
                System.out.println("文件夹:"+file.getAbsolutePath());  
            }else if(file.isFile()){  
                System.out.println("文件:"+file.getAbsolutePath());  
            }  
            System.out.println(jfc.getSelectedFile().getName());  
              
        }  
        
    
      
    }  
  • 相关阅读:
    解决Linux下gets函数警告
    freopen文件操作后控制台输入输出
    Linux C编程下没有 itoa()函数的问题
    codeforces 631A Interview
    codeforces 617BChocolate
    codeforces 630KIndivisibility(容斥原理)
    codeforces 633B A Trivial Problem
    codeforces 624B Making a String
    codeforce 621A(水题)
    codeforces 624A Save Luke(水题)
  • 原文地址:https://www.cnblogs.com/linfenghp/p/5977761.html
Copyright © 2011-2022 走看看