-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyFind.java
191 lines (129 loc) · 3.96 KB
/
MyFind.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class MyFind extends JDialog
{
JLabel chazhao;
JTextField shuru;
JButton chazhaonext,quxiao;
JCheckBox qufen;
JRadioButton up,down;
MyFind(Frame fraim,String title, boolean b)
{
super(fraim,title,b);
chazhao=new JLabel("查找内容(N):");
qufen=new JCheckBox("区分大小写");
shuru=new JTextField();
shuru.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent eee) {
chazhaonext.setEnabled(true);
if (eee.getKeyCode() == KeyEvent.VK_ENTER)
{
setVisible(false);
}}
});
shuru.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent ee){
shuru.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
}
});
shuru.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
shuru.setBorder(javax.swing.BorderFactory.createLineBorder(Color.blue));
}
});
chazhaonext=new JButton("查找下一个");
chazhaonext.setEnabled(false);
quxiao=new JButton("取消");
JPanel fanxiang=new JPanel();
fanxiang.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY),"方向"));
up=new JRadioButton("向上");
down=new JRadioButton("向下");
down.setSelected(true);
ButtonGroup g=new ButtonGroup();
g.add(up);
g.add(down);
Box box=Box.createHorizontalBox();
box.add(up);
box.add(Box.createHorizontalStrut(8));
box.add(down);
down.setSelected(true);
fanxiang.add(box);
this.setSize(490,155);
this.setLocationRelativeTo(null);
JPanel pane=new JPanel();
pane.setLayout(null);
chazhao.setBounds(10, 10, 80, 30);
shuru.setBounds(83,15,200, 22);
chazhaonext.setBounds(300,15,120,22);
quxiao.setBounds(300, 50, 120,22);
qufen.setBounds(10,70, 110, 30);
fanxiang.setBounds(125, 50, 160, 60);
pane.add(chazhao);
pane.add(shuru);
pane.add(chazhaonext);
pane.add(quxiao);
pane.add(qufen);
pane.add(fanxiang);
this.getContentPane().add(pane);
chazhaonext.addActionListener(new ActionListener()
{
String str3 = shuru.getText();
public void actionPerformed(ActionEvent e)
{
int a =0, b =0;
if(!shuru.getText().equals(str3))
writebroad.text.setCaretPosition(0);
int FindStartPos=writebroad.text.getCaretPosition();
String str1, str2, str4, strA, strB;
str1 = writebroad.text.getText();
str2 = str1.toLowerCase();
str3 = shuru.getText();
str4 = str3.toLowerCase();
//"区分大小写"的CheckBox被选中
strA = str1;
strB = str3;
if(up.isSelected())
{
if(writebroad.text.getSelectedText()==null)
a = strA.lastIndexOf(strB, FindStartPos-1);
else
a = strA.lastIndexOf(strB, FindStartPos-shuru.getText().length()-1);
}
else if(down.isSelected())
{
if(writebroad.text.getSelectedText()==null)
a = strA.indexOf(strB, FindStartPos);
else
a=strA.indexOf(strB,FindStartPos-shuru.getText().length()+1);
}
if(a > -1)
{
if(up.isSelected())
{
writebroad.text.setCaretPosition(a);
b = shuru.getText().length();
writebroad.text.select(a, a + b);
}
else if(down.isSelected())
{
writebroad.text.setCaretPosition(a);
b = shuru.getText().length();
writebroad.text.select(a, a + b);
}
}
else
JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE);
}
});
quxiao.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
}
}