设置打开或保存文件对话框的文件类型

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 1.设置打开或保存文件时弹出的对话框的文件类型选择。
 * 2.外观选择。
 * @author luckystar2008
 */
public class FileSaveExtensionChooser extends JFrame{
	private JButton btn = new JButton("保存文件");
	private JComboBox laf;
	private Vector<SupportedLaF> supportedLookAndFeel = new Vector<SupportedLaF>();
	
	static class SupportedLaF {
		String name;
		LookAndFeel laf;

		SupportedLaF(String name, LookAndFeel laf) {
			this.name = name;
			this.laf = laf;
		}

		public String toString() {
			return name;
		}
	}
	
	public FileSaveExtensionChooser() {
		setSize(300,200);
		setVisible(true);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());
		add(btn);
		add(new JLabel("选择外观:"));
		setLookAndFeel();
		add(laf);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		btn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				String msg = "Hello World!";
				JFileChooser jfc = new JFileChooser();
				FileNameExtensionFilter fnef = new FileNameExtensionFilter("TXT and INI Files (.txt, .ini)", "txt","ini");
				FileNameExtensionFilter txt = new FileNameExtensionFilter("TXT Files (.txt)", "txt");
				FileNameExtensionFilter ini = new FileNameExtensionFilter("INI Files (.ini)", "ini");
				
				jfc.addChoosableFileFilter(fnef);
				jfc.addChoosableFileFilter(txt);
				jfc.addChoosableFileFilter(ini);
				jfc.setDialogType(JFileChooser.SAVE_DIALOG);
				int res = jfc.showSaveDialog(FileSaveExtensionChooser.this);
				if (res == JFileChooser.APPROVE_OPTION) {
					File file = jfc.getSelectedFile();
					System.out.println(file.getAbsolutePath());
					try {
						BufferedWriter bw = new BufferedWriter(new FileWriter(file));
						bw.write(msg);
						bw.flush();
						bw.close();
						System.out.println("File Save Finished!");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
		laf.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				SupportedLaF slaf = (SupportedLaF) laf.getSelectedItem();
				try {
					UIManager.setLookAndFeel(slaf.laf);
					System.out.println("Set LookAndFeel To " + slaf.name);
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		pack();
	}
	
	public void setLookAndFeel() {
		UIManager.LookAndFeelInfo[] installedLafs = UIManager
				.getInstalledLookAndFeels();
		for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
			try {
				Class lnfClass = Class.forName(lafInfo.getClassName());
				LookAndFeel laf = (LookAndFeel) (lnfClass.newInstance());
				if (laf.isSupportedLookAndFeel()) {
					String name = lafInfo.getName();
					supportedLookAndFeel.add(new SupportedLaF(name,laf));
				}
			} catch (Exception e) { 
				continue;
			}
		}
		
		laf = new JComboBox(supportedLookAndFeel);
		laf.setSelectedIndex(0);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileSaveExtensionChooser t2 = new FileSaveExtensionChooser();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值