Java Swing之按钮点击选择文件与获取选中文件绝对路径

来源:互联网 发布:文件收发管理系统 源码 编辑:程序博客网 时间:2024/06/03 14:51
button.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent e) {                                                      //按钮点击事件JFileChooser chooser = new JFileChooser();             //设置选择器 chooser.setMultiSelectionEnabled(true);             //设为多选int returnVal = chooser.showOpenDialog(button);        //是否打开文件选择框System.out.println("returnVal="+returnVal);if (returnVal == JFileChooser.APPROVE_OPTION) {          //如果符合文件类型String filepath = chooser.getSelectedFile().getAbsolutePath();      //获取绝对路径System.out.println(filepath);System.out.println("You chose to open this file: "+ chooser.getSelectedFile().getName());  //输出相对路径}}});

1 0
原创粉丝点击