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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM