java中如何实现windows程序中那种点击一个按钮弹出一个目录设置窗口来设置目录? rt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 jFileChooser = new javax.swing.JFileChooser();下面是例程 import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;class FileChooserDemo1 implements ActionListener{ JFrame f = null; JLabel label = null; JTextArea textarea = null; JFileChooser fileChooser = null; public FileChooserDemo1() { f = new JFrame("FileChooser Example"); Container contentPane = f.getContentPane(); textarea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textarea); scrollPane.setPreferredSize(new Dimension(350,300)); JPanel panel = new JPanel(); JButton b1 = new JButton("新建文件"); b1.addActionListener(this); JButton b2 = new JButton("存储文件"); b2.addActionListener(this); panel.add(b1); panel.add(b2); label = new JLabel(" ",JLabel.CENTER); fileChooser = new JFileChooser("D:\\");//建立一个FileChooser对象,并指定D:的目录为默认文件对话框路径. contentPane.add(label,BorderLayout.NORTH); contentPane.add(scrollPane,BorderLayout.CENTER); contentPane.add(panel,BorderLayout.SOUTH); f.pack(); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new FileChooserDemo1(); } public void actionPerformed(ActionEvent e) { File file = null; int result; /*当用户按下"打开文件"按钮时,JFileChooser的showOpenDialog()方法会输出文件对话框,并利用 *setApproveButtonText()方法取代按钮上"Open"文字;以setDialogTitle()方法设置打开文件对话框Title名称. *当使用选择完后,会将选择结果存到result变量中. */ if (e.getActionCommand().equals("新建文件")) { fileChooser.setApproveButtonText("确定"); fileChooser.setDialogTitle("打开文件"); result = fileChooser.showOpenDialog(f); textarea.setText(""); /*当用户按下打开文件对话框的"确定"钮后,我们就可以利用getSelectedFile()方法取得文件对象.若是用户按下打 *开文件对话框的"Cancel"钮,则将在label上显示"你没有选择任何文件"字样. */ if (result == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); label.setText("您选择打开的文件名称为:"+file.getName()); } else if(result == JFileChooser.CANCEL_OPTION) { label.setText("您没有选择任何文件"); } FileInputStream fileInStream = null; if(file != null) { try{ //利用FileInputStream将文件内容放入此数据流中以便读取. fileInStream = new FileInputStream(file); }catch(FileNotFoundException fe){ label.setText("File Not Found"); return; } int readbyte; try{ //以read()方法读取FileInputStream对象内容,当返回值为-1时代表读完此数据流.将所读到的字符显示 //在textarea中. while( (readbyte = fileInStream.read()) != -1) { textarea.append(String.valueOf((char)readbyte)); } }catch(IOException ioe){ label.setText("读取文件错误"); } finally{//回收FileInputStream对象,避免资源的浪费. try{ if(fileInStream != null) fileInStream.close(); }catch(IOException ioe2){} } } } //实作写入文件的功能. if (e.getActionCommand().equals("存储文件")) { result = fileChooser.showSaveDialog(f); file = null; String fileName; //当用户没有选择文件,而是自己键入文件名称时,系统会自动以此文件名建立新文件. if (result == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); label.setText("您选择存储的文件名称为:"+file.getName()); } else if(result == JFileChooser.CANCEL_OPTION) { label.setText("您没有选择任何文件"); } //写入文件我们使用FileOutputStream,在这个范例中,我们写入文件的方式是将之前内容清除并重新写入.若你想把 //新增的内容加在原有的文件内容后面,你可以使用FileOutputStream(String name,Boolean append)这个构造函数. FileOutputStream fileOutStream = null; if(file != null) { try{ fileOutStream = new FileOutputStream(file); }catch(FileNotFoundException fe){ label.setText("File Not Found"); return; } String content = textarea.getText(); try{ fileOutStream.write(content.getBytes()); }catch(IOException ioe){ label.setText("写入文件错误"); } finally{ try{ if(fileOutStream != null) fileOutStream.close(); }catch(IOException ioe2){} } } } }} 我试了一下,这个是用于打开文件的我想实现“目录设置”功能,上面那个类只能指定文件ps:csdn不能帖图么 JB=jbuilder?跟这个有关系?没懂你意思 private File _prevFile;private File getDownloadDirectory() { JFileChooser chooser = new JFileChooser(); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (_prevFile != null) { chooser.setCurrentDirectory(_prevFile); } if (chooser.showOpenDialog(ApplicationContext.getMainFrame()) == JFileChooser.APPROVE_OPTION) { _prevFile = chooser.getSelectedFile(); return _prevFile; } return null; } TrayIcon 拖拽问题 正则零宽断言 大牛请进~~~~打开对话框时右键弹出的菜单列表 有关抽象类Graphics的问题 如何获取当前程序文件的路径 java 怎 么 样 播 放 一 个 .mp3 格式 或 其 它 音频格试 的文件! java做动态折线图 求正则表达式,形如:f(Max,A012[1],TAB01,A001[1],>,100) f(Max,A012[1],TAB01,A001[1],=,'aa') Java的网络连接 JTable的单元格能否隐藏不显示 如何保留double型小数点后两位有效数字,不足补零? 这句话是什么意思?super关键字怎么理解?
import java.awt.*;
import java.awt.event.*;
import java.io.*;class FileChooserDemo1 implements ActionListener
{
JFrame f = null;
JLabel label = null;
JTextArea textarea = null;
JFileChooser fileChooser = null; public FileChooserDemo1()
{
f = new JFrame("FileChooser Example");
Container contentPane = f.getContentPane();
textarea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(350,300));
JPanel panel = new JPanel();
JButton b1 = new JButton("新建文件");
b1.addActionListener(this);
JButton b2 = new JButton("存储文件");
b2.addActionListener(this);
panel.add(b1);
panel.add(b2);
label = new JLabel(" ",JLabel.CENTER);
fileChooser = new JFileChooser("D:\\");//建立一个FileChooser对象,并指定D:的目录为默认文件对话框路径.
contentPane.add(label,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(panel,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new FileChooserDemo1();
}
public void actionPerformed(ActionEvent e)
{
File file = null;
int result;
/*当用户按下"打开文件"按钮时,JFileChooser的showOpenDialog()方法会输出文件对话框,并利用
*setApproveButtonText()方法取代按钮上"Open"文字;以setDialogTitle()方法设置打开文件对话框Title名称.
*当使用选择完后,会将选择结果存到result变量中.
*/
if (e.getActionCommand().equals("新建文件"))
{
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(f);
textarea.setText(""); /*当用户按下打开文件对话框的"确定"钮后,我们就可以利用getSelectedFile()方法取得文件对象.若是用户按下打
*开文件对话框的"Cancel"钮,则将在label上显示"你没有选择任何文件"字样.
*/
if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
label.setText("您选择打开的文件名称为:"+file.getName());
}
else if(result == JFileChooser.CANCEL_OPTION)
{
label.setText("您没有选择任何文件");
}
FileInputStream fileInStream = null;
if(file != null)
{
try{
//利用FileInputStream将文件内容放入此数据流中以便读取.
fileInStream = new FileInputStream(file);
}catch(FileNotFoundException fe){
label.setText("File Not Found");
return;
}
int readbyte;
try{
//以read()方法读取FileInputStream对象内容,当返回值为-1时代表读完此数据流.将所读到的字符显示
//在textarea中.
while( (readbyte = fileInStream.read()) != -1)
{
textarea.append(String.valueOf((char)readbyte));
}
}catch(IOException ioe){
label.setText("读取文件错误");
}
finally{//回收FileInputStream对象,避免资源的浪费.
try{
if(fileInStream != null)
fileInStream.close();
}catch(IOException ioe2){}
}
}
}
//实作写入文件的功能.
if (e.getActionCommand().equals("存储文件"))
{
result = fileChooser.showSaveDialog(f);
file = null;
String fileName;
//当用户没有选择文件,而是自己键入文件名称时,系统会自动以此文件名建立新文件.
if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
label.setText("您选择存储的文件名称为:"+file.getName());
}
else if(result == JFileChooser.CANCEL_OPTION)
{
label.setText("您没有选择任何文件");
}
//写入文件我们使用FileOutputStream,在这个范例中,我们写入文件的方式是将之前内容清除并重新写入.若你想把
//新增的内容加在原有的文件内容后面,你可以使用FileOutputStream(String name,Boolean append)这个构造函数.
FileOutputStream fileOutStream = null;
if(file != null)
{
try{
fileOutStream = new FileOutputStream(file);
}catch(FileNotFoundException fe){
label.setText("File Not Found");
return;
} String content = textarea.getText();
try{
fileOutStream.write(content.getBytes());
}catch(IOException ioe){
label.setText("写入文件错误");
}
finally{
try{
if(fileOutStream != null)
fileOutStream.close();
}catch(IOException ioe2){}
}
}
}
}
}
我想实现“目录设置”功能,上面那个类只能指定文件
ps:csdn不能帖图么
跟这个有关系?
没懂你意思
private File getDownloadDirectory() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (_prevFile != null) {
chooser.setCurrentDirectory(_prevFile);
}
if (chooser.showOpenDialog(ApplicationContext.getMainFrame()) ==
JFileChooser.APPROVE_OPTION) {
_prevFile = chooser.getSelectedFile();
return _prevFile;
}
return null;
}