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

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());  //输出相对路径

}
}
});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章