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();
}
}
分享到:
相关推荐
MFC打开保存文件对话框.可供参考
内置的打开和保存文件对话框.rar 内置的打开和保存文件对话框.rar 内置的打开和保存文件对话框.rar
C# //打开对话框读取文件 //打开对话框保存文件
MFC 文件对话框-打开文件-保存文件MFC 文件对话框-打开文件-保存文件MFC 文件对话框-打开文件-保存文件 MFC 文件对话框-打开文件-保存文件MFC 文件对话框-打开文件-保存文件MFC 文件对话框-打开文件-保存文件 MFC ...
java打开或保存文件时,只显示相应文件类型的文件。
这个对话框可以配置为只允许选择一个文件,或可以选择多个文件。 2.使用SaveFileDialog,用户可以为要保存的文件指定一个文件名和浏览的路径。 3.PrintDialog用户选择一个打印机,并设置打印选项。 4.配置页面的...
对于打开保存文件对话框可以使用MSComDlg.CommonDialog对象,并且可以设置类型过滤,但是需要设置MaxFileSize属性,否则会出现错误。 提示:你可以先修改部分代码再运行,总体感觉打开选择文件对话框还是比较有用的...
#For_Educational_Purposes_Only VC/MFC 使用"文件"对话框打开和保存文件,适合初学VC/MFC的同学。
Java生成一个文件打开的文件选择对话框窗口,和Win系统的浏览文件窗口相似,使用Java代码模拟实现的窗口程序,相关代码... //显示保存文件对话框 File file = chooser.getSelectedFile(); //得到选择的文件
解决打开、保存文件对话框下拉框中文乱码CFileDialog DlgOpen(FALSE,"*.*",NULL, OFN_OVERWRITEPROMPT,strfilter,FromHandle(this->m_hWndhw),0,FALSE);
西门子WinCC 如何使用 VBS 调用文件打开对话框pdf,本文介绍了在WinCC 中如何使用 VBS 调用文件打开对话框,系统环境 XP SP3, IE 6.0 SP3。
vs2010,VC++语言,文件对话框实现打开保存文件路径功能
Qt QFileDialog 文件选择对话框 多选
用JFileChooser和FileDialog来显示对话框,FileDialog没研究出来怎么过滤文件,JFileChooser界面差了点 环境:eclipce3.7.2 jre1.6
文件对话框空间commanddialog的练习
VS2008下的一个小程序。类似于word中点击保存,然后打开一个保存文件对话框。当存在相同文件时,提示是否覆盖。
js弹出保存对话框
unity 通过Windows API 函数实现文件选择功能,文件打开功能,文件保存路径选择功能。显示一个对话框,允许用户选择文件夹。返回选定文件夹的路径。 显示一个标准的文件打开对话框,允许用户选择一个文件以供应用...
VB 纯API 打开保存对话框 Public Function FileDialog(FormObject As Form, SaveDialog As Boolean, ByVal Title As String, ByVal Filter As String, Optional ByVal FileName As String, Optional ByVal ...
修改flcop提供的代码,以适应Delphi xe10环境,版权归flcop所有