JAVA如何得到文件路径
JAVA的应用程序里,从单击浏览-->选择文件-->打开.得到选择的文件的地址.这是怎么实现的啊?谁有代码啊?急需要!...
JAVA的应用程序里,从单击浏览-->选择文件-->打开.得到选择的文件的地址.这是怎么实现的啊?谁有代码啊?急需要!
展开
3个回答
展开全部
这个我做过
我马上给你写个例子
你等一下
写出来了,下面是代码
====================================
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author 小虫
*/
public class TestFrame extends JFrame{
static JTextField TextField;
static TestFrame testFrame;
public static void main(String args[]){
testFrame=new TestFrame();
TextField=new JTextField(" ");
JButton button=new JButton("选择文件");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//--核心代码
JFileChooser chooser = new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"文本文档", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(testFrame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
TextField.setText(chooser.getSelectedFile().getAbsolutePath());
}
//--核心代码结束
}
});
Container contentPane=testFrame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(TextField);
testFrame.setSize(300,100);
testFrame.setVisible(true);
}
}
====================================
一、JFileChooser 类为用户选择文件提供了一种简单的机制
以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
二、FileNameExtensionFilter用来指定的扩展名集合进行过滤。文件的扩展名是指文件名最后一个“.”后面的部分。
名称不包含“.”的文件没有文件扩展名。文件扩展名的比较不区分大小写。
下例创建了一个 FileNameExtensionFilter,它将显示 jpg 文件:
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
JFileChooser fileChooser = ...;
fileChooser.addChoosableFileFilter(filter);
我马上给你写个例子
你等一下
写出来了,下面是代码
====================================
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author 小虫
*/
public class TestFrame extends JFrame{
static JTextField TextField;
static TestFrame testFrame;
public static void main(String args[]){
testFrame=new TestFrame();
TextField=new JTextField(" ");
JButton button=new JButton("选择文件");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//--核心代码
JFileChooser chooser = new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"文本文档", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(testFrame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
TextField.setText(chooser.getSelectedFile().getAbsolutePath());
}
//--核心代码结束
}
});
Container contentPane=testFrame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(TextField);
testFrame.setSize(300,100);
testFrame.setVisible(true);
}
}
====================================
一、JFileChooser 类为用户选择文件提供了一种简单的机制
以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
二、FileNameExtensionFilter用来指定的扩展名集合进行过滤。文件的扩展名是指文件名最后一个“.”后面的部分。
名称不包含“.”的文件没有文件扩展名。文件扩展名的比较不区分大小写。
下例创建了一个 FileNameExtensionFilter,它将显示 jpg 文件:
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
JFileChooser fileChooser = ...;
fileChooser.addChoosableFileFilter(filter);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用JFileChooser
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
public class FileChooserDemo extends JPanel {
static final long serialVersionUID = 5854418136127725290L;
public class ExtensionFilter extends FileFilter {
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[]) {
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++) {
String ext = extensions[i];
if (path.endsWith(ext)
&& (path.charAt(path.length() - ext.length()) == '.')) {
return true;
}
}
return false;
}
public String getDescription() {
return (description == null ? extensions[0] : description);
}
}
public FileChooserDemo() {
JButton jb = new JButton("Open File Viewer");
add(jb);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(".");
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileFilter type1 = new ExtensionFilter("Java source", ".java");
FileFilter type2 = new ExtensionFilter("Image files",
new String[] { ".jpg", ".gif", "jpeg", "xbm" });
FileFilter type3 = new ExtensionFilter("HTML files",
new String[] { ".htm", ".html" });
chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);
chooser.addChoosableFileFilter(type3);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type2); // Initial filter setting
int status = chooser.showOpenDialog(FileChooserDemo.this);
if (status == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
System.out.println(f);
}
}
};
jb.addActionListener(listener);
}
public static void main(String args[]) {
JFrame f = new JFrame("Enhanced File Example");
JPanel j = new FileChooserDemo();
f.getContentPane().add(j, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
public class FileChooserDemo extends JPanel {
static final long serialVersionUID = 5854418136127725290L;
public class ExtensionFilter extends FileFilter {
private String extensions[];
private String description;
public ExtensionFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFilter(String description, String extensions[]) {
this.description = description;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++) {
String ext = extensions[i];
if (path.endsWith(ext)
&& (path.charAt(path.length() - ext.length()) == '.')) {
return true;
}
}
return false;
}
public String getDescription() {
return (description == null ? extensions[0] : description);
}
}
public FileChooserDemo() {
JButton jb = new JButton("Open File Viewer");
add(jb);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(".");
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileFilter type1 = new ExtensionFilter("Java source", ".java");
FileFilter type2 = new ExtensionFilter("Image files",
new String[] { ".jpg", ".gif", "jpeg", "xbm" });
FileFilter type3 = new ExtensionFilter("HTML files",
new String[] { ".htm", ".html" });
chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);
chooser.addChoosableFileFilter(type3);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type2); // Initial filter setting
int status = chooser.showOpenDialog(FileChooserDemo.this);
if (status == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
System.out.println(f);
}
}
};
jb.addActionListener(listener);
}
public static void main(String args[]) {
JFrame f = new JFrame("Enhanced File Example");
JPanel j = new FileChooserDemo();
f.getContentPane().add(j, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询