--------------------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
void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public
void run() {

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
void mouseClicked(MouseEvent e) {

//创建一个JFileChooser对象

JFileChooser chooser=new JFileChooser();

/**

* 弹出一个保存文件的对话框

* 需要指定一个
父级窗体,或指定为 null

* 返回值是 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】

最新文章

  1. Win7精简成功后的总结
  2. MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )
  3. SQL语句建表、设置主键、外键、check、default、unique约束
  4. leetcode 107 Binary Tree Level Order Traversal II ----- java
  5. 详细js中(function(window,document,undefined))的作用
  6. HTML+CSS学习笔记 (10) - CSS格式化排版
  7. 含有自增序列的表中使用truncate与delete的不同结果
  8. 使用linq语句获取指定条数的记录
  9. Unable to instantiate activity ComponentInfo或java.lang.ClassNotFoundException: com.ibright.herolegen
  10. atcoder它A Mountaineer
  11. 《JS权威指南学习总结--6.3删除属性》
  12. C/C++语言简介之运算符
  13. 关于查询中查询无果,也不报错,inpout标签中的value属性为‘ ’的判断问题
  14. php 批量下载文件
  15. springboot+layui实现PC端用户的增删改查 & 整合mui实现app端的自动登录和用户的上拉加载 & HBuilder打包app并在手机端下载安装
  16. ionic1滑动时间选择器
  17. Python学习笔记 -- 第五章
  18. CSS选择器,选择器的优先级
  19. HTML学习笔记01-HTML简介
  20. 浅谈iOS中MVVM的架构设计

热门文章

  1. 基于事件驱动的前端通信框架(封装socket.io)
  2. linux中添加PHP的mongoDB支持扩展
  3. 51nod 1525 && CF566D
  4. Win7远程桌面_ZC01
  5. js中的可枚举属性与不可枚举属性
  6. fatal error C1071: unexpected end of file found in comment
  7. ReactJS结合ES6入门Template
  8. 谈MicroMessageTest的开始创建
  9. pyqt5信号与槽2
  10. Java进阶06 容器