文件选择器JFileChooser

JFileChooser 的默认界面比较丑,一般的使用方法如下图所示


这里我要简略的介绍两种文件选择的窗口  1、JFileChooser  2、FileDialog

JFileChooser 的通常用法如下:

JFileChooser chooser=new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置只能选择文件
				chooser.setMultiSelectionEnabled(false);//设置文件不可多选
				chooser.setFileFilter(new FileNameExtensionFilter("JPG文件", "jpg"));
				int result=chooser.showOpenDialog(SaveInMySql.this);
				if(result==JFileChooser.APPROVE_OPTION)//打开文件选择器
				{
					pf=new PictureFile();
					File f=chooser.getSelectedFile();
					String name=f.getName();
					textField.setText(name);
					pf.setPicture(f);
					pf.setFileName(name);
				}


效果如下图所示


如果想给其修饰一下,如下代码所示


JFileChooser chooser =new JFileChooser();
		String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(chooser);//使设置得界面风格生效
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只显示目录
		chooser.showOpenDialog(frame);


FileDialog  的使用如下


FileDialog dialog=new FileDialog(this);
		dialog.setVisible(true);
		String path=dialog.getDirectory();
		File f=new File(path);
		File[] fs=f.listFiles(new java.io.FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				// TODO Auto-generated method stub
				if(pathname.getName().endsWith("tem"))
				{
					return true;
				}				
				else
				{
					return false;
				}
				
			}
		});

比较简单就不添加注释了




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值