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()); //輸出相對路徑
}
}
});
Java Swing之按鈕點擊選擇文件與獲取選中文件絕對路徑
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.