求关于Jfilechooser选择保存路径的代码,我的意思就是:File file= new file("路径"),现在那个路径我通过jfilechooser选择,求代码
解决方案 »
- 想不明白这个问题:JAVA IO
- FileDialog对话框类打开对话框后能不能修改“保存(或者打开)类型”的文本框内的文字-->比如默认为“所有文件(*.*)”,修改成“Microsoft Office Excel 工作簿( *.xls)”
- 输入输出txt文档转换问题,在线等.
- 请问在使用replaceAll(),如何区别空白字符和空格?
- 关于Object中的clone()方法`
- 第2次问了,各位帮忙!是java的图片处理问题
- 请教一个关于socket的小问题!
- 2的30次方,java怎么表示阿?
- 简单问题,请指教!!
- 为何会有"not found in class java.awt.Button"错误???
- 求助,几个小问题
- 谁会把iava文件转成exe我下了个exe4j不知道怎么用?
import java.io.*;
import java.awt.*;
import java.awt.event.*;public class FileChooserDemo {
private JFileChooser chooser = new JFileChooser();
private JButton saveButton = new JButton("Save");
private JLabel label = new JLabel(" ");
public FileChooserDemo(){
JFrame frame = new JFrame("JFileChooser Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//选择目录,选择文件的话注释掉这行。
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int retValue = chooser.showSaveDialog(saveButton);
if(retValue == JFileChooser.APPROVE_OPTION){
label.setText(chooser.getSelectedFile().toString());
}
}
}); frame.getContentPane().add(saveButton,BorderLayout.CENTER);
frame.getContentPane().add(label,BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(200,100));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
new FileChooserDemo();
}
});
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;public class FileChooserDemo {
private JFileChooser chooser = new JFileChooser();
private JButton saveButton = new JButton("Save");
private JLabel label = new JLabel(" ");
public FileChooserDemo(){
JFrame frame = new JFrame("JFileChooser Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//选择目录,选择文件的话注释掉这行。
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int retValue = chooser.showSaveDialog(saveButton);
if(retValue == JFileChooser.APPROVE_OPTION){
label.setText(chooser.getSelectedFile().toString());
}
}
}); frame.getContentPane().add(saveButton,BorderLayout.CENTER);
frame.getContentPane().add(label,BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(200,100));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
new FileChooserDemo();
}
});
}
}
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.JTextField;
public class Test extends JFrame implements ActionListener {
JPanel pnlMain;
JTextField txtfile;
JButton btnSelect;
JFileChooser fc = new JFileChooser();
public Test() {
pnlMain = new JPanel();
this.getContentPane().add(pnlMain);
txtfile = new JTextField(10);
btnSelect = new JButton("选择");
btnSelect.addActionListener(this);
pnlMain.add(txtfile);
pnlMain.add(btnSelect);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSelect) {
fc.setSelectedFile(new File(txtfile.getText()));
int intRetVal = fc.showOpenDialog(this);
if (intRetVal == JFileChooser.APPROVE_OPTION) {
txtfile.setText(fc.getSelectedFile().getPath());
}
}
}
public static void main(String[] args) {
JFrame f = new Test();
f.setSize(200, 300);
f.setVisible(true);
}
}这样?