📄 findtext.java
字号:
/*
* FindText.java
*
* Created on 2007年12月10日, 下午5:45
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package WordTest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import WordTest.*;
/**
*
* @author Administrator
*/
public class FindText extends JDialog implements ActionListener {
private JTextField FindTxt=new JTextField(20);
private JLabel ShowText=new JLabel("查找内容:");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JButton FindNext=new JButton("查找下一个");
private JButton FindEnter=new JButton("确定");
//private JButton FindCancel=new JButton("取消");
String str,strnext;
int startp,endp,nexttemp,newstartp,newendp;
/** Creates a new instance of FindText */
public FindText(JFrame fra,String str) {
super(fra,str);
this.setLocation(300,200);
p1.setLayout(new FlowLayout());
p1.add(ShowText);
p1.add(FindTxt);
p2.setLayout(new FlowLayout());
p2.add(FindNext);
p2.add(FindEnter);
// p2.add(FindCancel);
add("North",p1);
add("South",p2);
setSize(400,100);
this.show();
FindNext.addActionListener(this);
FindEnter.addActionListener(this);
// FindCancel.addActionListener(this);
}
// <editor-fold defaultstate="collapsed" desc=" 查找事件区 ">
void FindNextEvent()
{
nexttemp = newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall = MainFrame.TextArea.getText();
MainFrame.TextArea.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext = MainFrame.TextArea.getSelectedText();
newstartp = strnext.indexOf(FindTxt.getText()) + nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp = newstartp + FindTxt.getText().length();
MainFrame.TextArea.select(newstartp,newendp); ////找到相应文本,并选择
NoFind();
}
void FindEnterEvent()
{
if(!FindTxt.getText().equals(""))
{
this.dispose();
}
else
{
str = MainFrame.TextArea.getText();
//返回FindTxt.getText()对应字符在当前字符串中出现的饿第一个位置
startp = str.indexOf(FindTxt.getText());//
endp = startp + FindTxt.getText().length();
MainFrame.TextArea.select(startp,endp);//选中
newendp = endp;////////获取这次查找的终点
NoFind();
}
}
void NoFind()
{
if(!MainFrame.TextArea.getSelectedText().equals(FindTxt.getText()))
{
MainFrame.TextArea.setCaretPosition(0); //光标返回文件头部
JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找结果",JOptionPane.ERROR_MESSAGE);
}
}
// </editor-fold>
public void actionPerformed(ActionEvent e) {
if(e.getSource()==FindNext)
{
FindNextEvent();
}
else if(e.getSource()==FindEnter)
{
FindEnterEvent();
}
// else if(e.getSource()==FindCancel)
//{ this.hide();
// this.dispose();
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -