轉自:http://hi.baidu.com/gglzf4/blog/item/a950a402ff7ae67b3812bba2.html#0
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能選擇檔案夾
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//只能選擇檔案
int flag = fileChooser.showOpenDialog(this);
if(flag ==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
attachHorizontalList.addNewAttaches(file);
jScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
// jScrollPane.setBorder(BorderFactory.createTitledBorder("共"+(++count)+"份附件"));
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
jScrollPane.setOpaque(false);
jScrollPane.getViewport().setOpaque(false);
this.add(jScrollPane);
this.repaint();
}
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int flag = chooser.showSaveDialog(mainFrame);
if (flag == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getAbsolutePath()+attachmentName;
try {
FileInputStream ins = new FileInputStream(
attachmentFile);
FileOutputStream out = new FileOutputStream(path);
byte b[] = new byte[512];
while ((ins.read(b)) > 0) {
out.write(b);
}
out.close();
ins.close();
} catch (IOException e2) {
JOptionPane.showMessageDialog(null, "儲存失敗");
e2.printStackTrace();
}
}
開啟對話方塊和儲存對話方塊的設計 swing java
1.點擊開啟按鈕後,開啟一個對話方塊
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//產生一個檔案選取器
jChooser = new JFileChooser();
//設定預設的開啟目錄,如果不設的話按照window的預設目錄(我的文件)
jChooser.setCurrentDirectory(new File("e:/"));
//設定開啟檔案類型,此處設定成只能選擇檔案夾,不能選擇檔案
jChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能開啟檔案夾
//開啟一個對話方塊
int index = jChooser.showDialog(null, "開啟檔案");
if (index == JFileChooser.APPROVE_OPTION) {
//把擷取到的檔案的絕對路徑顯示在文本編輯框中
jt.setText(jChooser.getSelectedFile().getAbsolutePath());
readPath = jt.getText() + "\\";
}
}
});
2.點擊儲存按鈕後,開啟一個儲存對話方塊
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jChooser2 = new JFileChooser();
jChooser2.setCurrentDirectory(new File("e:/"));//設定預設開啟路徑
jChooser2.setDialogType(JFileChooser.SAVE_DIALOG);//設定儲存對話方塊
//將設定好了的兩種檔案過濾器添加到檔案選取器中來
TxtFileFilter txtFileFilter = new TxtFileFilter();
XlsFileFilter xlsFileFilter = new XlsFileFilter();
jChooser2.addChoosableFileFilter(txtFileFilter);
jChooser2.addChoosableFileFilter(xlsFileFilter);
int index = jChooser2.showDialog(null, "儲存檔案");
if (index == JFileChooser.APPROVE_OPTION) {
File f = jChooser2.getSelectedFile();
String fileName = jChooser2.getName(f) + ".xls";
writePath = jChooser2.getCurrentDirectory().getAbsolutePath() + fileName;
try {
writeFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//程式執行完畢後,出現一個對話方塊來提示
int option = JOptionPane.showConfirmDialog(null, "賤人:結果已產生", "結果",
JOptionPane.YES_NO_OPTION);
System.exit(0);
}
}
});
//重寫檔案過濾器,設定開啟類型中幾種可選的檔案類型,這裡設了兩種,一種txt,一種xls
class TxtFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.txt(文字檔)";
}
}
class XlsFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".xls");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.xls(表格檔案)";
}
}