想实现一个 获得文件路径的功能,类似于 操作系统下 查询文件 时候 选择路径的 功能。
不知道有没有控件能直接用。
jFileChooser的时候,有一个问题没有解决,jFileChooser控件在选择到了文件的时候才能点击“确定”触发事件,如果能在选择到目录的时候点击“确定”触发事件,问题就解决了。
谢谢各位!
不知道有没有控件能直接用。
jFileChooser的时候,有一个问题没有解决,jFileChooser控件在选择到了文件的时候才能点击“确定”触发事件,如果能在选择到目录的时候点击“确定”触发事件,问题就解决了。
谢谢各位!
解决方案 »
- Java中实现深复制的两种方法
- 请教各位有经验的学长,大连的最好
- 急! J2SE、J2ME网络编程问题!
- Jc编译运行都没错,JB编译没错,但是运行时全部都是NullPointerException
- 请教:.dat文件转byte数组时,数据是乱码,如何转中文?
- 页面的加了这个上传的enctype="multipart/form-data,怎么取字段的数组值,不能request.getParameterValues(""),急!!!
- 高手请进:关于Java跨平台问题
- 如何在linux下设置Java环境变量?谢谢
- 迷茫的初学者
- FranceRevelution Calendar 与 GregorianCalendar 的转换问题,急!
- 写一个简单的JAVA程序,实现INSERT USER(USER_NO,USER_NAME)VALUES(5,'JOM')这个SQL??
- 面试题!在线等!
JFileChooser
setFileSelectionModepublic void setFileSelectionMode(int mode)
Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY. Parameters:
mode - the type of files to be displayed:
JFileChooser.FILES_ONLY
JFileChooser.DIRECTORIES_ONLY
JFileChooser.FILES_AND_DIRECTORIES
Throws:
IllegalArgumentException - if mode is an illegal Dialog mode
import java.awt.event.*;
import java.io.*;public class FileViewer extends Frame implements ActionListener {
String directory;
TextArea textarea;
public FileViewer() { this(null, null); }
public FileViewer(String filename) { this(null, filename); }
public FileViewer(String directory, String filename) {
super();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { dispose(); }
});
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add("Center", textarea);
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
this.add(p, "South");
Font font = new Font("SansSerif", Font.BOLD, 14);
Button openfile = new Button("Open File");
Button close = new Button("Close");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
close.addActionListener(this);
close.setActionCommand("close");
close.setFont(font);
p.add(openfile);
p.add(close);
this.pack();
if (directory == null) {
File f;
if ((filename != null)&& (f = new File(filename)).isAbsolute()) {
directory = f.getParent();
filename = f.getName();
}
else directory = System.getProperty("user.dir");
}
this.directory = directory;
setFile(directory, filename);
}
public void setFile(String directory, String filename) {
if ((filename == null) || (filename.length() == 0)) return;
File f;
FileReader in = null; try {
f = new File(directory, filename);
in = new FileReader(f);
char[] buffer = new char[4096];
int len;
textarea.setText("");
while((len = in.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
textarea.append(s);
}
this.setTitle("FileViewer: " + filename);
textarea.setCaretPosition(0);
}
catch (IOException e) {
textarea.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("FileViewer: " + filename + ": I/O Exception");
}
finally { try { if (in!=null) in.close(); } catch (IOException e) {} }
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("open")) { FileDialog f = new FileDialog(this, "Open File", FileDialog.LOAD);
f.setDirectory(directory);
f.show(); directory = f.getDirectory();
setFile(directory, f.getFile());
f.dispose();
}
else if (cmd.equals("close"))
this.dispose();
}
static public void main(String[] args) throws IOException {
Frame f = new FileViewer((args.length == 1)?args[0]:null);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) { System.exit(0); }
});
f.show();
}
}运行一下看看,不知道用不用的上?