使用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 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】

原文地址:https://www.cnblogs.com/siwuxie095/p/6677703.html