在對話框的基礎模式上添加JFileChooser。讀取檔案,顯示在文字編輯框中。并可以儲存編輯框中的内容,選擇指定檔案位置。
package Night;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.GridLayout;
public class FileChooserDemo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileChooserDemo window = new FileChooserDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FileChooserDemo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 553, 371);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("\u8BF7\u9009\u62E9\u6587\u4EF6\uFF1A");
frame.getContentPane().add(label, BorderLayout.NORTH);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(0, 1, 0, 0));
JTextArea textArea = new JTextArea();
panel.add(textArea);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("打開");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc = new JFileChooser();
int value = jfc.showOpenDialog(frame);
if(value==JFileChooser.APPROVE_OPTION)//選擇打開
{
File fileName = jfc.getSelectedFile();
try {
FileReader fread = new FileReader(fileName);
char[] ch = new char[1024];
int hasData=0;
try {
if((hasData=fread.read(ch))>=0) {
textArea.append(new String(ch));
}
fread.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("儲存");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc = new JFileChooser();
int value = jfc.showOpenDialog(frame);
if(value==JFileChooser.APPROVE_OPTION) {
File filename = jfc.getSelectedFile();
try {
FileWriter fw = new FileWriter(filename);
char[] ch = new char[1024];
int hasData=0;
fw.write(textArea.getText());
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
});
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("清除");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText("");
}
});
panel_1.add(btnNewButton_2);
}
}