📄 finder.java
字号:
package notepad.popGUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
/**
* 类名Finder<BR>
* 查找对话框类,查到后把文字设置为被选中状态,没有查到给出提示。 <BR>
*
* @author 黎明你好
* @version 2.0 2009-4-27
*/
public class Finder extends JDialog implements ActionListener, ItemListener,
CaretListener
{
/**序列化时为了保持版本的兼容性*/
private static final long serialVersionUID = 1L;
/** 标签,提示信息位"查找内容:" */
private JLabel find_label;
/** 用于输入查询内容的文本框 */
private JTextField inputFindString_text;
/** 按钮,查找下一个,取消 */
private JButton findNext_button, cancel_button;
/** 用于区分大小写的选择框 */
private JCheckBox big_or_small;
/** 标签,显示内容位"方向" */
private JLabel findAspect_label;
/** 设置查询方向的单选按钮 */
private JRadioButton upFind, downFind;
/** 用于布局的panel */
private JPanel panel1, panel2;
/** 用于使upFind,downFind按钮单选 */
private ButtonGroup group;
/** 查询的文本区对象 */
private JTextArea findTextArea;
/** 在这个字符串上查找啊 */
private String findString;
/** 要查询的字符串 */
private String stringToFind;
/** 保存最后输入的查询字符串 */
private String lastText = "";
/** 查找的相对位置 */
private int findDownIndex = 0, findUpIndex;
/** 搜索方向,true为向下,false为向上,默认向下; */
private boolean findAspect = true;
/** 是否区分大小写,true位区分,fasle位不区分,默认位区分; */
private boolean isBM = false;
/** 当前JDialog的指定所有者窗体 */
private JFrame frm;
/**
* 类的构造方法
*
* @param parent -
* JFrame 用来确定这个对话框是基于谁显示的
* @param textArea -
* JTextArea 文本区,就是被查询对象,
*/
public Finder(JFrame parent, JTextArea textArea)
{
super(parent, "查找", false);
frm = parent;
this.findTextArea = textArea;
findString = findTextArea.getText().toLowerCase();
find_label = new JLabel("查找内容(N)");
inputFindString_text = new JTextField(16);// 设置成之前查询过的字符串
findNext_button = new JButton("查找下一个(F)");
cancel_button = new JButton("取消");
big_or_small = new JCheckBox("区分大小写(C)", false);
findAspect_label = new JLabel(" 方向:");
upFind = new JRadioButton("向上(U)", false);
downFind = new JRadioButton("向下(D)", true);
panel1 = new JPanel();
panel2 = new JPanel();
group = new ButtonGroup();
inputFindString_text.requestFocus();// 输入框得到焦点
inputFindString_text.selectAll();// 输入框文字是全选状态
findNext_button.setEnabled(false);// 按钮开始不可用
/* 设置键盘助记符,和下划线 */
find_label.setDisplayedMnemonicIndex(5);
findNext_button.setMnemonic(KeyEvent.VK_F);
findNext_button.setDisplayedMnemonicIndex(6);
big_or_small.setMnemonic(KeyEvent.VK_C);
big_or_small.setDisplayedMnemonicIndex(6);
upFind.setMnemonic(KeyEvent.VK_U);
upFind.setDisplayedMnemonicIndex(3);
downFind.setMnemonic(KeyEvent.VK_D);
downFind.setDisplayedMnemonicIndex(3);
/* 添加监控 */
inputFindString_text.addActionListener(this);
findNext_button.addActionListener(this);
cancel_button.addActionListener(this);
inputFindString_text.addCaretListener(this);// 用于侦听有没有文本框输入字符
big_or_small.addItemListener(this);
upFind.addItemListener(this);
downFind.addItemListener(this);
group.add(upFind);// 单选按钮
group.add(downFind);
/* 把各个组建添加上来 */
panel1.add(find_label);
panel1.add(inputFindString_text);
panel1.add(findNext_button);
panel2.add(big_or_small);
panel2.add(findAspect_label);
panel2.add(upFind);
panel2.add(downFind);
panel2.add(cancel_button);
this.setLayout(new GridLayout(2, 1));
this.add(panel1);
this.add(panel2);
this.setSize(450, 120);
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)// 点击查询下一个按钮后
{
stringToFind = inputFindString_text.getText().trim();
if (isBM)// 如果区分大小写,重新获得字符串
{
findString = findTextArea.getText();
stringToFind = inputFindString_text.getText().trim();
}
else
/* 不区分的时候,都转换成小写字母 */
{
findString = findString.toLowerCase();// 都转换成小写字母,这样就是不区分大小写了
stringToFind = stringToFind.toLowerCase();
}
if (stringToFind.compareTo(lastText) != 0)// 如果查找字符串改变,重新进行匹配
{
lastText = stringToFind;// 保存最后一次查询的内容
int nowIndex = findTextArea.getCaretPosition();// 光标位置
if (nowIndex > 0)// 如果光标不在文件头,则从光标位置开始搜索
{
findDownIndex = nowIndex;
findUpIndex = nowIndex;
}
else
{
findDownIndex = 0;
findUpIndex = 0;
}
}
if (findAspect)// 向下搜索
{
findDown(stringToFind);
}
else if (!findAspect)// 向上搜索
{
findUp(stringToFind);
}
}
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;
}
}
if (e.getSource() == upFind || e.getSource() == downFind)// 用来设置向那个方向搜索
{
if (upFind.isSelected())// 向上搜索
{
findAspect = false;
}
else if (downFind.isSelected())// 向下搜索
{
findAspect = true;
}
}
}
/**
* 用于向下搜索的方法
*/
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; // 改变下一次,向下搜索时候的开始位置
findUpIndex = index - n;// 改变向上搜索的开始位置
}
else
{
java.awt.Toolkit.getDefaultToolkit().beep();// 发出一个音频嘟嘟声。
JOptionPane.showMessageDialog(null, "找不到" + textToFind, "记事本",
JOptionPane.INFORMATION_MESSAGE);// 淡出提示对话框,找不到
}
}
}
/**
* 用于向上搜索的方法
*/
public void findUp(String textToFind)
{
int n = textToFind.length();// 得到要搜索字符串的长度
if (!"".equals(textToFind))// 当搜索的字符串不是空的时候
{
int index = findString.lastIndexOf(textToFind, findUpIndex);// 从findUpIndex位置开始向上查询textToFind在findString中的位置
if (index != -1)
{
frm.transferFocus();// 查找到后,让JFrame得到焦点
findTextArea.setCaretPosition(index);
findTextArea.select(index, index + n);// 把查到的字符串,设置成被选择状态
findUpIndex = index - n; // 改变下一次,向上搜索的开始位置
findDownIndex = index + n;// 改变向下搜索的开始位置
}
else
{
java.awt.Toolkit.getDefaultToolkit().beep();// 发出一个音频嘟嘟声。
JOptionPane.showMessageDialog(frm, "找不到" + textToFind, "记事本",
JOptionPane.INFORMATION_MESSAGE);// 淡出提示对话框,找不到
}
}
}
/**
* 监听器方法,发生操作调用时,检查输入文本框是否有字符,没有的时候确定按钮不可按,有的时候可按
*/
public void caretUpdate(CaretEvent e)
{
JTextField tf = (JTextField) e.getSource();
if (!"".equals(tf.getText()))// 用于设置,当输入文本为空,按钮不可按。
{
findNext_button.setEnabled(true);
}
if ("".equals(tf.getText()))// 当不为空时,按钮可按
{
findNext_button.setEnabled(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -