/**
* @author godhuyang 2010年7月17日 星期六
*/
package GUIDemos;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
class FileChooser implements ActionListener{
private JFrame frame;
private JTextArea text;
private JLabel label;
private JButton btnOpen;
private JButton btnSave;
private JFileChooser chooser = new JFileChooser();
public FileChooser(){
frame=new JFrame("FileChooser Demo");
frame.setLayout(new BorderLayout(3,3));
text=new JTextArea(8,10);
label=new JLabel("文本编辑中...");
btnOpen=new JButton("打开");
btnSave=new JButton("保存");
JPanel pan=new JPanel();
pan.add(btnOpen);
pan.add(btnSave);
JScrollPane sp=new JScrollPane(text);
btnOpen.addActionListener(this);
btnSave.addActionListener(this);
// ((Object) frame).setOpacity(90/100.0f);
frame.add(label,BorderLayout.NORTH);
frame.add(sp,BorderLayout.CENTER);
frame.add(pan,BorderLayout.SOUTH);
frame.setSize(800,600);
frame.setLocation(100,50);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnOpen){
label.setText("打开文件");
chooser.setApproveButtonText("确定");
chooser.setApproveButtonToolTipText("I Love You");
chooser.setDialogTitle("寻找我的文件");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Txt Files", "txt", "*.*");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
label.setText("你已选择打开"+chooser.getSelectedFile().getName());
String filePath=chooser.getSelectedFile().getAbsolutePath();
//System.out.println(filePath);
File f=new File(filePath);
InputStream is=null;
//显示文件内容
try {
is=new FileInputStream(f);
int temp=0;
while((temp=is.read())!=-1){
text.append(""+(char)temp);
}
is.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}else if(returnVal==JFileChooser.CANCEL_OPTION){
label.setText("It/'s have been canceled.");
}
}else if(e.getSource()==btnSave){
//设置必要信息
label.setText("保存文件");
chooser.setApproveButtonText("确定");
chooser.setApproveButtonToolTipText("I Love You");
chooser.setDialogTitle("保存我的文件");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Txt Files", "txt", "*.*");
chooser.setFileFilter(filter);
int returnVal=chooser.showSaveDialog(frame);
if(returnVal==JFileChooser.APPROVE_OPTION){//确定保存了
String filePath=chooser.getSelectedFile().getAbsolutePath();
String fileName=chooser.getSelectedFile().getName();
//判断文件名是否符合后缀名为txt,正则表达式实现
Pattern p=Pattern.compile("(.*)+//.(txt)$");
Matcher m=p.matcher(fileName);
if(!m.matches())
filePath=filePath+".txt";
//保存文件
File f=new File(filePath);
try {
PrintStream ps=new PrintStream(f);
ps.print(text.getText());
ps.close();
label.setText("You have saved the file.");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}else{
label.setText("You have canceled to save the file");
}
}
}
public class FileChooseDemo {
public static void main(String args[]){
new FileChooser();
}
}