类似windows的文件路径选择的对话框在java如何实现 我想在一按钮上加载监听,就弹出类似windows的文件路径选择的对话框,用于保存文件路径。请问大虾们如何实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JFileChooserhow to use it:http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html public class FileSelector { private FileFilter acceptFileType ; private FileFilter acceptListType; private Frame frame = new Frame(); private JFileChooser fileChooser; public FileSelector(){ acceptFileType = new javax.swing.filechooser.FileFilter() { public boolean accept(File file) { //可接受的文件类型 String name = file.getName().toLowerCase(); return name.endsWith(".mp3")|| name.endsWith(".MP3")|| name.endsWith(".wav")|| name.endsWith(".WAV")|| file.isDirectory(); } public String getDescription() { //文件描述 return "mp3,wav,midi 音乐文件"; } }; acceptListType = new javax.swing.filechooser.FileFilter(){ public boolean accept(File file){ String name = file.getName().toLowerCase(); return name.endsWith(".list")|| name.endsWith(".LIST")|| file.isDirectory(); } public String getDescription(){ return "java版媒体播放器专用播放列表"; } }; fileChooser = new JFileChooser(FileSystemView.getFileSystemView()); } File openFile(){ fileChooser.setFileFilter(acceptFileType); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(false); fileChooser.setDialogTitle("打开单个文件"); int result = fileChooser.showOpenDialog(frame); if(result == JFileChooser.APPROVE_OPTION){ return fileChooser.getSelectedFile(); } else{ return null; } } File[] openMultiFile(){ fileChooser.setFileFilter(acceptFileType); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(true); fileChooser.setDialogTitle("打开多个文件"); int result = fileChooser.showOpenDialog(frame); if(result == JFileChooser.APPROVE_OPTION){ File[] files = fileChooser.getSelectedFiles(); return files; } else{ return null; } } File[] openDirectory(){ File file; fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle("打开整个目录"); int result = fileChooser.showOpenDialog(frame); if(result == JFileChooser.APPROVE_OPTION){ file = fileChooser.getSelectedFile(); if(file==null||file.getName().equals("")){ JOptionPane.showMessageDialog(frame, "错误的路径", "出错了",JOptionPane.ERROR_MESSAGE); return null; } return file.listFiles(new FilenameFilter(){ public boolean accept(File dir,String name){ return name.indexOf(".mp3")!=-1|| name.indexOf(".MP3")!=-1|| name.indexOf(".wav")!=-1|| name.indexOf(".WAV")!=-1; } }); } else return null; }} javax.swing.JFileChooser;JFileChooser jf=new JFileChooser();jf.showSaveDialog(null);或者if.showSaveDialog(容器); public class FileChooserDemo { static private final String newline = "\n"; JButton openButton, saveButton; JTextArea log; JFileChooser fc; JFrame jf = null; public FileChooserDemo(){ jf = new JFrame(); Container cp = jf.getContentPane(); log = new JTextArea(5,20); log.setMargin(new Insets(5,5,5,5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); fc = new JFileChooser(); openButton = new JButton("Open a File..."); openButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog(jf); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); log.append("Opening: " + file.getName() + "." + newline); } else { log.append("Open command cancelled by user." + newline); } } }); saveButton = new JButton("Save a File..."); saveButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int returnVal = fc.showSaveDialog(jf); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); log.append("Saving: " + file.getName() + "." + newline); } else { log.append("Save command cancelled by user." + newline); } }}); JPanel buttonPanel = new JPanel(); buttonPanel.add(openButton); buttonPanel.add(saveButton); cp.add(buttonPanel, BorderLayout.PAGE_START); cp.add(logScrollPane, BorderLayout.CENTER); jf.setVisible(true); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String []args){ new FileChooserDemo(); }} 貼失誤了 把我那個里的file.getName() 要改成 file.getAbsolutePath() 呵呵 就可以 JLabel 问题………… Java中 有没有类似于 Js中的 Interval 的方法,或者setTimeout java中使用matlab 如何得知一个实例在初始化的时候是否使用了类型参数 tomcat最近总是当机 如何在JTextField中加文字,用鼠标点击JTextField时就消失的那种 谢谢,追分~ 怎样在java中实现启动画面? 怎么设置成相对路径,access.... java中,invoke如何翻译???????????????? 请问jbuilder有没有GridControl组件? java入门 要分的进来!!!!
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
private FileFilter acceptFileType ;
private FileFilter acceptListType;
private Frame frame = new Frame();
private JFileChooser fileChooser;
public FileSelector(){
acceptFileType = new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) { //可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".mp3")||
name.endsWith(".MP3")||
name.endsWith(".wav")||
name.endsWith(".WAV")||
file.isDirectory();
}
public String getDescription() { //文件描述
return "mp3,wav,midi 音乐文件";
}
};
acceptListType = new javax.swing.filechooser.FileFilter(){
public boolean accept(File file){
String name = file.getName().toLowerCase();
return name.endsWith(".list")||
name.endsWith(".LIST")||
file.isDirectory();
}
public String getDescription(){
return "java版媒体播放器专用播放列表";
}
};
fileChooser = new JFileChooser(FileSystemView.getFileSystemView());
}
File openFile(){
fileChooser.setFileFilter(acceptFileType);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("打开单个文件");
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
return fileChooser.getSelectedFile();
}
else{
return null;
}
}
File[] openMultiFile(){
fileChooser.setFileFilter(acceptFileType);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setDialogTitle("打开多个文件");
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
File[] files = fileChooser.getSelectedFiles();
return files;
}
else{
return null;
}
}
File[] openDirectory(){
File file;
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("打开整个目录");
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
if(file==null||file.getName().equals("")){
JOptionPane.showMessageDialog(frame, "错误的路径", "出错了",JOptionPane.ERROR_MESSAGE);
return null;
}
return file.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
return name.indexOf(".mp3")!=-1||
name.indexOf(".MP3")!=-1||
name.indexOf(".wav")!=-1||
name.indexOf(".WAV")!=-1;
}
});
}
else
return null;
}
}
JFileChooser jf=new JFileChooser();
jf.showSaveDialog(null);或者if.showSaveDialog(容器);
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
JFrame jf = null;
public FileChooserDemo(){
jf = new JFrame();
Container cp = jf.getContentPane();
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
openButton = new JButton("Open a File...");
openButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(jf);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
} }
});
saveButton = new JButton("Save a File...");
saveButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(jf);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
}});
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
cp.add(buttonPanel, BorderLayout.PAGE_START);
cp.add(logScrollPane, BorderLayout.CENTER);
jf.setVisible(true);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String []args){
new FileChooserDemo();
}
}