JavaSwing FileInput

try
        {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            //UIManager.put("RootPane.setupButtonVisible", false);
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            
        }
        catch(Exception e)
        {
            //TODO exception
        }
        
        // 创建窗体对象
        JFrame jFrame =new JFrame();
        // 设置窗体大小
        jFrame.setSize(800, 500);
        // 设置窗体全屏展示
        //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        // 设置窗体显示位置
        //jFrame.setLocation(100,200);
        // 设置窗体显示正中间
        jFrame.setLocationRelativeTo(null);
        // 设置窗体标题
        jFrame.setTitle("窗体标题");
        // 设置窗体不可全屏显示
        //jFrame.setResizable(false);
        // 设置窗体关闭后退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置此窗口是否应该始终位于其他窗口上方
        jFrame.setAlwaysOnTop(true);
        // 设置窗体图标
        jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage());
        

        
        JPanel panel =new JPanel(null);
        
        
        JButton btn01 =new JButton("打开");
        btn01.setLocation(10,10);
        btn01.setSize(100, 40);
        
        JTextField text =new JTextField();
        text.setLocation(120,10);
        text.setSize(300, 40);
        
        
        btn01.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 创建文件选择器
                JFileChooser fileChooser =new JFileChooser();
                
                // 创建文件选择器 (指定直接进入到磁盘目录)
                JFileChooser fileChooser1 =new JFileChooser("D:/");
                
                
                // 设置选择模式 FILES_ONLY 只选择文件(默认)
                // 设置选择模式 DIRECTORIES_ONLY 只选择文件夹
                // 设置选择模式 FILES_AND_DIRECTORIES 文件夹和文件均可选择
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                // 设置一次可选择多个文件
                fileChooser.setMultiSelectionEnabled(true);
                
                // 弹出文件选择器,返回值是用户点击的确定,还是取消按钮
                // APPROVE_OPTION表示点击的是确定按钮
                int option=fileChooser.showOpenDialog(jFrame);
                
                
                // 选择单个文件
                if(option ==JFileChooser.APPROVE_OPTION) {
                    File file =fileChooser.getSelectedFile();
                    text.setText(file.getAbsolutePath());
                }else {
                    text.setText("");
                }
                
                // 选择多个文件
                if(option ==JFileChooser.APPROVE_OPTION) {
                    File[] files =fileChooser.getSelectedFiles();
                    String filename="";
                    for(int i=0;i<files.length;i++) {
                        filename=files[i].getAbsolutePath()+";";
                    }
                    text.setText(filename);
                }else {
                    text.setText("");
                }
                
            }
        });
        
        
        panel.add(btn01);
        panel.add(text);
        
        jFrame.add(panel);
        // 设置窗体可见
        jFrame.setVisible(true);
            
        

 

posted @ 2024-12-21 19:22  leungqingyun  阅读(7)  评论(0)    收藏  举报