设置打开或保存文件对话框的文件类型
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 EX-QINCIDONG001 * */ 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); getsavefilename 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 = SelectedFile(); System.out.AbsolutePath()); 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(ClassName()); LookAndFeel laf = (LookAndFeel) (wInstance()); if (laf.isSupportedLookAndFeel()) { String name = Name(); 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(); } } |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论