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

EventQueue.invokeLater(new Runnable() {

publicvoid 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

publicvoid 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】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值