使用JFileChooser保存文件
--------------------siwuxie095
工程名:TestFileChooser
包名:com.siwuxie095.filechooser
类名:TestSave.java
工程结构目录如下:
代码:
package com.siwuxie095.filechooser; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; /** * JFileChooser 是 Java Swing 框架中的文件选择器, * 在应用程序中经常会遇到打开文件和保存文件等操作, * 文件选择器 JFileChooser 是专门应对这种情况而出现的 * * @author siwux * */ public class TestSave extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static EventQueue.invokeLater(new Runnable() { public try { TestSave frame = new TestSave(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public TestSave() {
try { UIManager.setLookAndFeel(new WindowsLookAndFeel()); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); }
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane);
JButton btnSave = new JButton("Save"); btnSave.addMouseListener(new MouseAdapter() { @Override public //创建一个JFileChooser对象 JFileChooser chooser=new JFileChooser(); /** * 弹出一个保存文件的对话框 * 需要指定一个 * 返回值是 int 类型,创建以接收返回值 */ int value=chooser.showSaveDialog(TestSave.this);
/** * 如果返回 APPROVE_OPTION,也可以使用对象调用,即 chooser.APPROVE_OPTION * * 说明有文件被成功返回,即 * 这里实际上是将一个不存在的文件包装成了一个假设存在的文件,然后将之返回 * 但该文件并没有真实的被创建,仅仅是创建了一个文件对象,并可设定路径 * 需要使用 createNewFile() 创建文件 */ if (value==JFileChooser.APPROVE_OPTION) {
//打印返回文件的绝对路径 //System.out.println(chooser.getSelectedFile().getAbsolutePath());
try {
File newFile=chooser.getSelectedFile();
if (!newFile.exists()) { newFile.createNewFile(); System.out.println(newFile.getAbsolutePath()); }
} catch (IOException e1) { e1.printStackTrace(); } }
} }); btnSave.setFocusable(false); contentPane.add(btnSave, BorderLayout.NORTH); } } |
将窗体
JFrame 的 LookAndFeel 设定为 Windows
在根面板
contentPane 的上方添加一个 JButton
将
JButton 的 focusable 属性设为
false,并将其文本(text)改为:
Save,Rename 为:btnSave
为 JButton 添加 mouseClicked 事件,点击 按钮 弹出对话框
运行程序:
【made by siwuxie095】
最新文章
- Win7精简成功后的总结
- MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )
- SQL语句建表、设置主键、外键、check、default、unique约束
- leetcode 107 Binary Tree Level Order Traversal II ----- java
- 详细js中(function(window,document,undefined))的作用
- HTML+CSS学习笔记 (10) - CSS格式化排版
- 含有自增序列的表中使用truncate与delete的不同结果
- 使用linq语句获取指定条数的记录
- Unable to instantiate activity ComponentInfo或java.lang.ClassNotFoundException: com.ibright.herolegen
- atcoder它A Mountaineer
- 《JS权威指南学习总结--6.3删除属性》
- C/C++语言简介之运算符
- 关于查询中查询无果,也不报错,inpout标签中的value属性为‘ ’的判断问题
- php 批量下载文件
- springboot+layui实现PC端用户的增删改查 &; 整合mui实现app端的自动登录和用户的上拉加载 &; HBuilder打包app并在手机端下载安装
- ionic1滑动时间选择器
- Python学习笔记 -- 第五章
- CSS选择器,选择器的优先级
- HTML学习笔记01-HTML简介
- 浅谈iOS中MVVM的架构设计