遍历文件夹中的文件 对话框选择文件 案例 制作chm文件时用的一个小程序(二)

2014-11-24 08:39:15 · 作者: · 浏览: 7
me) {
int val = JOptionPane.showConfirmDialog(frame, "确定离开吗 ");
if (val == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
//选择文件对话框
package com.mengdian.findandreplace.ui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.mengdian.findandreplace.service.FindAndReplaceService;
public class FindAndReplaceFrame extends JFrame {
private String filePath = null;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
/**
*
*/
private static final long serialVersionUID = 1L;
public FindAndReplaceFrame(){
init();
}
/**
* 初始化界面方法
*/
private void init() {
//窗口标题
this.setTitle("批量对文件进行查找替换");
//设置大小
this.setSize(800,500);
//剧中
this.setLocationRelativeTo(null);
//加 组件
this.setContentPane(createContentPanel());
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//点击关闭按钮退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
super.windowClosing(e);
}
});
}
/**
* 创建主窗口
* @return
*/
private JPanel createContentPanel() {
JPanel panel = new JPanel(new FlowLayout(100,200,10));
final JTextField jtf = new JTextField("-------------------------文件绝对路径-------------------------");
jtf.setEditable(false);
JButton jbt = new JButton("选择文件/文件夹");
panel.add(jtf,BorderLayout.CENTER);
panel.add(jbt,BorderLayout.SOUTH);
jbt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = jfc.showOpenDialog(jfc);
if(returnVal == JFileChooser.APPROVE_OPTION){
filePath = jfc.getSelectedFile().getAbsolutePath();
jtf.setText(filePath);
File file = new File(filePath);
try {
FindAndReplaceService.replace(file);
//弹出替换成功对话框
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换成功","消息",JOptionPane.CLOSED_OPTION);
//点击确定按钮推出
} catch (Exception ee) {
JOptionPane.showConfirmDialog(FindAndReplaceFrame.this, "替换失败");
ee.printStackTrace();
}
}
}
});
return panel;
}