package org.rocking;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
public class MainWindow extends JFrame {
/**
*
*/
private static final long serialVersionUID = -4793216655531089765L;
BorderLayout borderLayout = null;
FlowLayout flowLayout = null;
JFrame frame = new JFrame("");
JPanel mainPanel;
JPanel northPanel;
JPanel centerPanel;
JPanel southPanel;
JButton button = new JButton();
JButton processBtn = new JButton();
JTextField textField = new JTextField(30);
JTextField oldTextField = new JTextField(47);//旧字符串
JTextField newTextField = new JTextField(47);//新字符串
File file = null;
String filePath = "";
static String lastpath = "";//上次路径
Properties properties = new Properties();
public static final Logger logger = Logger.getLogger(MainWindow.class);
public MainWindow() {
//初始化
//--------------------------------------------------------------------------------
//创建布局管理器
//--------------------------------------------------------------------------------
borderLayout = new BorderLayout();
flowLayout = new FlowLayout();
filePath = this.getClass().getResource("/conf.properties").getPath();
lastpath = FileManager.readPropertyValue(filePath, "org.rocking.lastPath");
logger.info("filePath: " + filePath);
logger.info("lastpath: " + lastpath);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(lastpath); //对话框
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int i = fileChooser.showOpenDialog(getContentPane()); //opendialog
if (i == JFileChooser.APPROVE_OPTION) { //判断是否为打开的按钮
file = fileChooser.getSelectedFile(); //取得选中的文件
textField.setText(file.getPath()); //取得路径
lastpath = file.getPath();
FileManager.writeProperties(filePath, "org.rocking.lastPath", lastpath);
}
}
});
button.setText("选择路径");
button.setSize(50, 20);
processBtn.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
boolean tag = true;
if(file != null) {
String oldString = oldTextField.getText();
String newString = newTextField.getText();
if(oldString != null && newString != null) {
try {
new ReplaceAction().execute(file.getAbsolutePath(), oldString, newString);
} catch (IOException e1) {
tag = false;
e1.printStackTrace();
}
} else {
tag = false;
}
} else {
tag = false;
}
JLabel label = new JLabel();
JDialog dialog = new JDialog(frame, "提示信息", true);
dialog.setSize(50, 30);
dialog.setBounds(300, 300, 250, 200);
dialog.getContentPane().add(label);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
if(tag) {
label.setText("处理成功!");
} else {
label.setText("处理失败!");
}
dialog.show();
}
});
processBtn.setText("替换");
processBtn.setSize(50, 20);
JLabel oldTextlabel = new JLabel();
oldTextlabel.setText("旧串");
JLabel newTextlabel = new JLabel();
newTextlabel.setText("新串");
northPanel = new JPanel();
northPanel.setLayout(flowLayout);
northPanel.add(button); //布局处理
northPanel.add(textField);
centerPanel = new JPanel();
centerPanel.setLayout(flowLayout);
centerPanel.add(oldTextlabel);
centerPanel.add(oldTextField);
centerPanel.add(newTextlabel);
centerPanel.add(newTextField);
southPanel = new JPanel();
southPanel.setLayout(flowLayout);
southPanel.add(processBtn);
mainPanel = (JPanel) getContentPane();
mainPanel.setLayout(borderLayout);
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(southPanel, BorderLayout.SOUTH);
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("icon.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
frame.setIconImage(image);
frame.getContentPane().add(mainPanel);
frame.setTitle("字符串批量替换");
/* Image icon = Toolkit.getDefaultToolkit().getImage("");
frame.setIconImage(icon);//去掉图标
*/ frame.setSize(600, 270);
frame.setVisible(true);
frame.setBounds(300, 300, 600, 270);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MainWindow();
}
}
鹏_鹏
- 粉丝: 50
- 资源: 15
最新资源
- 运用python生成的跳跃的爱心
- 基于 Java 实现的 Socket.IO 服务器 实时 Java 框架.zip
- 基于 Ant 的 Java 项目示例.zip
- 各种字符串相似度和距离算法的实现Levenshtein、Jaro-winkler、n-Gram、Q-Gram、Jaccard index、最长公共子序列编辑距离、余弦相似度…….zip
- 运用python生成的跳跃的爱心
- 包括用 Java 编写的程序 欢迎您在此做出贡献!.zip
- (源码)基于QT框架的学生管理系统.zip
- 功能齐全的 Java Socket.IO 客户端库,兼容 Socket.IO v1.0 及更高版本 .zip
- 功能性 javascript 研讨会 无需任何库(即无需下划线),只需 ES5 .zip
- 分享Java相关的东西 - Java安全漫谈笔记相关内容.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈