📄 finddialog.java
字号:
package javadesign;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.border.*;
/*************************************************
* 类FindDialog用来进行查找
*
***********************************************/
public class FindDialog extends JDialog implements ActionListener{
private JLabel findLabel=new JLabel("输入要查找的内容:");
public static JTextField findText = new JTextField(15);
private JButton buttonNext = new JButton("查找下一个");
private ButtonGroup group=new ButtonGroup();
private JButton buttonCancel = new JButton("取消");
private JRadioButton findUp=new JRadioButton("向上",false);
private JRadioButton findDown=new JRadioButton("向下",true);
private JCheckBox letter=new JCheckBox("区分大小写",false);
private Container container=this.getContentPane();
private JPanel findPanel=new JPanel();
private JPanel buttonPanel=new JPanel();
private JPanel aspectPanel=new JPanel();
Pattern pattern ;
// 用Pattern类的matcher()方法生成一个Matcher对象
public static Matcher matcher ;
boolean begin=true ,letterflag=true,updownflag=false;
int judge=Pattern.CASE_INSENSITIVE; //区分大小写的值为1,不区别为2,此时judge=2
int startp,len,count=1;
public FindDialog(Frame frame,String title,boolean modal){
super(frame,title,modal);
this.setSize(380, 150);
this.setResizable(false);
setLocation(Note.xp+ 50, Note.yp+ 150);
buttonNext.setEnabled(false);
findText.selectAll(); //全选
findText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(findText.getText().length()!=0)
buttonNext.setEnabled(true);
else
buttonNext.setEnabled(false);
}
});
group.add(findUp);
group.add(findDown);
container.setLayout(null);
findPanel.setLayout(new GridLayout(1,2));
buttonPanel.setLayout(new GridLayout(2,1,0,5));
aspectPanel.setLayout(new GridLayout(1,2));
findPanel.add(findLabel);
findPanel.add(findText);
buttonPanel.add(buttonNext);
buttonPanel.add(buttonCancel);
aspectPanel.add(findUp);
aspectPanel.add(findDown);
findPanel.setBounds(10,10,250,25);
buttonPanel.setBounds(265,10,100,50);
letter.setBounds(10,70,100,20);
aspectPanel.setBounds(120,50,140,50);
//建立类型为凸起的边框
Border border=BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
//建立边框式为border,标题为"方向"的TitledBorder
aspectPanel.setBorder(BorderFactory.createTitledBorder(border,"方向"));
container.add(findPanel);
container.add(buttonPanel);
container.add(letter);
container.add(aspectPanel);
this.getRootPane().setDefaultButton(buttonNext); // 默认回车按钮//
buttonNext.addActionListener(this);
findUp.addActionListener(this);
findDown.addActionListener(this);
buttonCancel.addActionListener(this);
letter.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonNext) {
Note.selectflag=false;
switch(count) {
case 0:String leave=Note.display.getText().substring(0,Note.display.getCaretPosition());
if(leave.matches(findText.getText())||Note.display.getCaretPosition()-len<=0) {
JOptionPane.showMessageDialog(null,"找不到 \""+findText.getText()+"\"","文本编辑器",JOptionPane.WARNING_MESSAGE);
int i=Note.display.getText().indexOf(findText.getText());
Note.display.select(i,i+findText.getText().length());
break;
}
Note.display.select(0, Note.display.getCaretPosition()-len);
startp=Note.display.getSelectedText().lastIndexOf(findText.getText());
len=findText.getText().length();
Note.display.select(startp, startp+len);
break;
case 1:if(begin){
try{
Note.selectflag=false;
//编译模式,参数findText.getText()表示输入的正则表达式,judge表示模式类型
pattern=Pattern.compile(findText.getText(),judge);
//获取匹配器,Note.display.getText()为待处理的字符串
matcher = pattern.matcher(Note.display.getText()); //用Pattern类的matcher()方法生成一个Matcher对象
begin=false;
}catch(Exception ev){}
}
if (matcher.find(Note.display.getCaretPosition())) //从光标处开始匹配模式
Note.display.select(matcher.start(), matcher.end());
else
JOptionPane.showMessageDialog(null,"找不到 \""+findText.getText()+"\"","文本编辑器",JOptionPane.WARNING_MESSAGE);
break;
}
} else if(e.getSource()==findUp)
count=0;
else if(e.getSource()==findDown){
begin=true;
count=1;
} else if(e.getSource()==buttonCancel)
this.setVisible(false);
else if(e.getSource()==letter) {
if(letterflag){judge=1;letterflag=false;} else {judge=2;letterflag=true;}
begin=true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -