import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class SystemOS extends JFrame implements ActionListener{
JPanel pnlMain;
JTextField txtfile;
JButton btnSelect;
JFileChooser fc = new JFileChooser();
public SystemOS() {
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){
/*
这是尤为重要的。因为JFileChooser默认的是选择文件,而需要选目录。
故要将DIRECTORIES_ONLY装入模型
另外,若选择文件,则无需此句
*/
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int intRetVal = fc.showOpenDialog(this);
if( intRetVal == JFileChooser.APPROVE_OPTION){
txtfile.setText(fc.getSelectedFile().getPath());
}
}
}
public static void main(String[] args){
JFrame f = new SystemOS();
f.setSize(200,300);
f.setVisible(true);
}
}
Java Swing学习:文件选择框
Java为文件选择框提供了JFileChooser类API。通过使用该类,可以很方便的现实文件选择对话框。见下例:
//建立文件选择框对象 JFileChooser fc=new JFileChooser(); //设定文件选择框标题 fc.setDialogTitle("Open class File"); //显示文件选择框,在选择后将结果储存到returnVal变量中 int returnVal = fc.showOpenDialog(app.getFrameInstance()); //如果用户选择了文件,并点击了"Opne/打开"按钮,显示用户选择的文件全名路径, //如果用户点击"Close/关闭"按钮,以及其它方式退出文件选择框,则什么也不做。 if (returnVal == JFileChooser.APPROVE_OPTION){ File file = fc.getSelectedFile(); System.out.println(file.getPath()); }
注意在文件选择对话框中,可以进行选择目录的操作,只需要用FileChooser对象的setFileSelectionMode()方法指定其选择模式为目录选择。见下面代码
//上例中可以在文件对话框显示语句前加上下面代码,使对话框成为目录选择对话框 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
在文件选择过程中,我们可以为文件选择对话框设置过滤器,有了过滤器的对话框,会用下拉列表的方式将过滤的文件名特征显示在"文件类型"组合框中,并将不符合条件的文件名和目录名过滤掉(不显示)。
为一个文件选择对话框设置过滤器需要定义过滤器类,见下例:
public class ClassFileFilter extends FileFilter{ public boolean accept(File f) { String filename = f.getName(); return filename.endsWith(".class"); } public String getDescription() { return "*.class"; } }
该类定义了一个过滤Java类文件"*.class"的过滤器。将它增加到文件选择对话框中,可以过滤出以.class为尾缀的文件。见下面代码:
fc.addChoosableFileFilter(new ClassFileFilter());
针对一个对话框,我们可以为其加装多个过滤器。这里有对过滤器更加诠释的代码: public class ImageFilter extends FileFilter { //Accept all directories and all gif, jpg, tiff, or png files. public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = Utils.getExtension(f); if (extension != null) { if (extension.equals(Utils.tiff) || extension.equals(Utils.tif) || extension.equals(Utils.gif) || extension.equals(Utils.jpeg) || extension.equals(Utils.jpg) || extension.equals(Utils.png)) { return true; } else { return false; } } return false; } public String getDescription() { return "Just Images"; } } 该过滤器为图像文件过滤器。
2.---------------------------------------------------------------------------
public class jCutter extends JFrame {
private JTextField textField;
private JButton button;
private JFileChooser fDialog;
private JFrame frame;
.
.
.
button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String msg;
fDialog=new JFileChooser(); //文件选择器
int result=fDialog.showOpenDialog(frame);
if(result==JFileChooser.APPROVE_OPTION){
String fname=fDialog.getName(fDialog.getSelectedFile());
frame.setTitle(fname);
msg="File Open Approved";
}else
msg="File Open Cancelled";
JOptionPane.showMessageDialog(frame,msg); //提示框
}
});