📄 replace.java
字号:
package notepad.popGUI;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
/**
* 类名Replace<BR>
* 用来替换的类。<BR>
* 先查找后在替换,也可以全部替换。<BR>
*
* @version 2.0 2009-4-27<BR>
*
* @author 黎明你好
*/
public class Replace extends JDialog implements ActionListener, ItemListener,
CaretListener
{
/** 序列化时为了保持版本的兼容性 */
private static final long serialVersionUID = 1L;
/** 标签,提示信息位"查找内容:", "替换为:" */
private JLabel find_label, replace_label;
/** 用于输入查询内容的文本框,查询内容、替换内容 */
private JTextField inputFindString_text, inputReplaceString_text;
/** 用于设置是否区分大小写的选择框 */
private JCheckBox big_or_small;
/** 按钮,查找下一个,取消 */
private JButton findNext_button, cancel_button;
/** 按钮,替换,全部替换 */
private JButton replace_button, replaceAll_button;
/** 用于布局的panel */
private JPanel panel1, panel2;
/** 查询的文本区对象 */
private JTextArea findTextArea;
/** 在这个字符串上查询 */
private String findString;
/** 要查询的字符串 */
private String textToFind;
/** 保存最后输入的查询字符串 */
private String lastText = "";
/** 查找的相对位置 */
private int findDownIndex = 0;
/** 是否区分大小写,true位区分,fasle位不区分,默认位区分; */
private boolean isBM = false;
/** 是否查询到要替换的字符串,默认为没查到 */
private boolean isFindString = false;
/** 当前JDialog的指定所有者窗体 */
private JFrame frm;
/**
* 构造方法
*
* @param parent
* JFrame 用来确定这个对话框是基于谁显示的
* @param textArea
* JTextArea 文本区,就是被查询对象,
*/
public Replace(JFrame parent, JTextArea textArea)
{
super(parent, "替换", false);
frm = parent;
this.findTextArea = textArea;
findString = findTextArea.getText().toLowerCase();
find_label = new JLabel("查找内容(N):");
replace_label = new JLabel("替换为(P): ");
inputFindString_text = new JTextField(16);
inputReplaceString_text = new JTextField(16);
findNext_button = new JButton("查找下一个(F)");
cancel_button = new JButton("取消");
replace_button = new JButton("替换(R)");
replaceAll_button = new JButton("全部替换(A)");
big_or_small = new JCheckBox("区分大小写(C)", false);
panel1 = new JPanel();
panel2 = new JPanel();
inputFindString_text.requestFocus();// 输入框得到焦点
findNext_button.setEnabled(false);// 按钮开始不可用
replace_button.setEnabled(false);
replaceAll_button.setEnabled(false);
// 设置键盘助记符,和下划线
find_label.setDisplayedMnemonicIndex(5);
replace_label.setDisplayedMnemonicIndex(4);
findNext_button.setMnemonic(KeyEvent.VK_F);
findNext_button.setDisplayedMnemonicIndex(6);
big_or_small.setMnemonic(KeyEvent.VK_C);
big_or_small.setDisplayedMnemonicIndex(6);
replace_button.setMnemonic(KeyEvent.VK_R);
replace_button.setDisplayedMnemonicIndex(3);
replaceAll_button.setMnemonic(KeyEvent.VK_A);
replaceAll_button.setDisplayedMnemonicIndex(5);
// 添加监控
inputFindString_text.addActionListener(this);
findNext_button.addActionListener(this);
cancel_button.addActionListener(this);
replace_button.addActionListener(this);
replaceAll_button.addActionListener(this);
inputFindString_text.addCaretListener(this);// 用于侦听有没有文本框输入字符
big_or_small.addItemListener(this);
// 把各个组建添加上来
panel1.setLayout(new FlowLayout());
panel1.add(find_label);
panel1.add(inputFindString_text);
panel1.add(replace_label);
panel1.add(inputReplaceString_text);
panel2.setLayout(new GridLayout(4, 1, 5, 5));
panel2.add(findNext_button);
panel2.add(replace_button);
panel2.add(replaceAll_button);
panel2.add(cancel_button);
this.setLayout(null);// 空布局,要设置精确坐标,和长宽
this.add(panel1);
this.add(panel2);
this.add(big_or_small);
panel1.setBounds(0, 10, 320, 70);
panel2.setBounds(330, 10, 150, 110);
big_or_small.setBounds(30, 90, 120, 20);
this.setSize(500, 180);
this.setLocationRelativeTo(parent);// 设置窗口相对于指定组件的位置
this.setResizable(false);
this.validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
dispose();
}
});
}
/**
* 监听器方法,发生操作调用时
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == inputFindString_text)// 如果输入完后按回车
{
findNext_button.doClick();// 使按钮点了一下
}
if (e.getSource() == findNext_button)// 点击查询下一个按钮后
{
textToFind = inputFindString_text.getText().trim();
if (isBM)// 如果区分大小写,重新获得字符串
{
findString = findTextArea.getText();
textToFind = inputFindString_text.getText().trim();// 去掉前后空格的字符串
}
else
// 不区分的时候,都转换成小写字母
{
findString = findString.toLowerCase();// 都转换成小写字母,这样就是不区分大小写了
textToFind = textToFind.toLowerCase();
}
if (textToFind.compareTo(lastText) != 0)// 如果查找字符串改变,重新进行匹配
{
lastText = textToFind;// 保存最后一次查询的内容
int nowIndex = findTextArea.getCaretPosition();// 光标位置
if (nowIndex > 0)// 如果光标不在文件头,则从光标位置开始搜索
findDownIndex = nowIndex;
else
findDownIndex = 0;
}
findDown(textToFind);// 开始查找
}
if (e.getSource() == replace_button)// 点击替换后,替换字符串
{
if (isFindString == true)
{
replaceSelectString();
}
}
if (e.getSource() == replaceAll_button)// 点击替换后,替换字符串
{
while (isFindString == true)
{
replaceSelectString();
findDown(textToFind);// 开始查找
}
}
if (e.getSource() == cancel_button)// 点击取消按钮后
{
this.setVisible(false);
this.dispose();
}
}
/**
* 监听器方法,发生操作调用时,设置查询时,是否区分大小写
*/
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == big_or_small)
{
if (big_or_small.isSelected())
{
isBM = true;
}
else if (!big_or_small.isSelected())
{
isBM = false;
}
}
}
/**
* 监听器方法,发生操作调用时,检查输入文本框是否有字符,没有的时候确定按钮不可按,有的时候可按
*/
public void caretUpdate(CaretEvent e)
{
JTextField tf = (JTextField) e.getSource();
if (!"".equals(tf.getText()))// 用于设置,当输入文本为空,按钮不可按。
{
findNext_button.setEnabled(true);
replace_button.setEnabled(true);
replaceAll_button.setEnabled(true);
}
if ("".equals(tf.getText()))// 当不为空时,按钮可按
{
findNext_button.setEnabled(false);
replace_button.setEnabled(false);
replaceAll_button.setEnabled(false);
}
}
/**
* 用于向下搜索的方法
*
* @param textToFind
* 到要搜索的字符串
*/
public void findDown(String textToFind)
{
int n = textToFind.length();// 得到要搜索字符串的长度
if (!"".equals(textToFind))// 当搜索的字符串不是空的时候
{
int index = findString.indexOf(textToFind, findDownIndex);// 从findDownIndex开始向下查询textToFind在findString中的位置
if (index != -1)
{
frm.transferFocus();// 查找到后,让JFrame得到焦点
findTextArea.setCaretPosition(index);
findTextArea.select(index, index + n);// 把查到的字符串,设置成被选择状态
findDownIndex = index + n; // 改变下一次,向下搜索时候的开始位置
isFindString = true;// 查到了
}
else
{
isFindString = false;// 没查到
java.awt.Toolkit.getDefaultToolkit().beep();// 发出一个音频嘟嘟声。
JOptionPane.showMessageDialog(null, "找不到" + textToFind, "记事本",
JOptionPane.INFORMATION_MESSAGE);// 淡出提示对话框,找不到
}
}
}
/**
* 替换字符串的方法
*/
public void replaceSelectString()
{
int start = findTextArea.getSelectionStart();
int end = findTextArea.getSelectionEnd();// 得到在textArea里,被选中字符串的起始,终止位置
findTextArea
.replaceRange(inputReplaceString_text.getText(), start, end);// 进行替换操作
findDownIndex = findTextArea.getCaretPosition();// 下次查询位置是从刚才替换的字符串头开始的
findString = findTextArea.getText();// 因为替换过字符串,所有要重新获得下字符串
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -