📄 finddialog.java
字号:
package notebook;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Button;
import java.awt.Panel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Label;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
public class FindDialog extends JDialog implements ActionListener {
JPanel panel1 = new JPanel();
Button btnFind = new Button();
XYLayout xYLayout1 = new XYLayout();
Label label1 = new Label();
JTextField jtxtFind = new JTextField();
JCheckBox jcbMatchCase = new JCheckBox();
JRadioButton jrbUp = new JRadioButton();
JRadioButton jrbDown = new JRadioButton();
Button btnCance = new Button();
ButtonGroup buttonGroup1 = new ButtonGroup();
NoteBook mainFrame = null;
int FindStartPos = 0; //查找初始位置
int FindLength = 0;//文本框中文总长度
public FindDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public FindDialog(Frame parent) {
super(parent);
try {
mainFrame = (NoteBook)parent;
FindLength = mainFrame.textArea.getText().length();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public FindDialog() {
this(new Frame(), "MainFrame_FindBox", false);
}
private void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
this.setTitle("查找");
btnFind.setLabel("查找下一个");
panel1.setPreferredSize(new Dimension(355, 115));
panel1.setToolTipText("");
label1.setText("查找内容");
jcbMatchCase.setText("区分大小写");
jrbUp.setText("向上");
jrbDown.setSelected(true);
jrbDown.setText("向下");
btnCance.setLabel("取消");
this.getContentPane().add(panel1, java.awt.BorderLayout.EAST);
panel1.add(label1, new XYConstraints(20, 20, -1, -1));
panel1.add(jtxtFind, new XYConstraints(90, 17, 119, -1));
panel1.add(jcbMatchCase, new XYConstraints(15, 55, -1, -1));
panel1.add(jrbUp, new XYConstraints(122, 55, -1, -1));
panel1.add(jrbDown, new XYConstraints(183, 55, -1, -1));
panel1.setBackground(SystemColor.control);
jcbMatchCase.setBackground(SystemColor.control);
jrbUp.setBackground(SystemColor.control);
jrbDown.setBackground(SystemColor.control);
buttonGroup1.add(jrbUp);
buttonGroup1.add(jrbDown);
panel1.add(btnCance, new XYConstraints(243, 49, 97, -1));
panel1.add(btnFind, new XYConstraints(243, 15, 97, -1));
btnCance.addActionListener(this);
btnFind.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCance)
this.dispose();
else if(e.getSource()==btnFind)//查找
{
int a = 0, b = 0;
String str1,str2,str3,str4,strA,strB;
str1 = mainFrame.textArea.getText();
str2 = str1.toLowerCase();
str3 = this.jtxtFind.getText();//查找的字符串
str4 = str3.toLowerCase();//转化成小写的查找字符
mainFrame.strFind=str3;//为主窗口查找字符串赋值
if (jcbMatchCase.isSelected()==true)
{
strA = str1;
strB = str3;
}
else
{
strA = str2;
strB = str4;
}
if (jrbUp.isSelected()==true)
{
a = strA.lastIndexOf(strB, FindLength-FindStartPos);
if (a > -1) {
mainFrame.textArea.setCaretPosition(a);
b = jtxtFind.getText().length();
mainFrame.textArea.select(a,a+b);
FindStartPos =FindLength-a+1;
mainFrame.FindStartPos= FindStartPos;//为主窗口的查找位置赋值
}
else
{
JOptionPane.showMessageDialog(null, "找不到\"" + str3+"\".","记事本", 1);
FindStartPos=0;
}
}
if (jrbDown.isSelected()==true)
{
a = strA.indexOf(strB, FindStartPos);
if (a > -1)
{
mainFrame.textArea.setCaretPosition(a);
b = jtxtFind.getText().length();
mainFrame.textArea.select(a, a + b);
FindStartPos = a + b;
mainFrame.FindStartPos= FindStartPos;//为主窗口的查找位置赋值
} else {
JOptionPane.showMessageDialog(null, "找不到\"" + str3 + "\".",
"记事本", 1);
FindStartPos = 0;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -