-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindAll.java
203 lines (125 loc) · 3.94 KB
/
findAll.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
192
193
194
195
196
197
198
199
200
201
202
203
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.awt.*;
import java.nio.Buffer;
import java.util.Iterator;
import java.util.TreeMap;
public class findAll extends JDialog
{
int t;
JLabel chazhao;
JTextField shuru;
JButton chazhaonext,quxiao;
JCheckBox qufen;
JRadioButton up,down;
static int h,w;
static String search;
String findposition="它们位于:\n";
static Highlighter highlighter;
findAll(Frame frame,String title, boolean b)
{
super(frame,title,b);
chazhao=new JLabel("查找内容(N):");
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("取消");
this.setSize(405,120);
this.setLocationRelativeTo(null);
JPanel pane=new JPanel();
pane.setLayout(null);
chazhao.setBounds(10, 10, 80, 30);
shuru.setBounds(10,40,200, 22);
chazhaonext.setBounds(230,15,120,22);
quxiao.setBounds(230, 50, 120,22);
pane.add(chazhao);
pane.add(shuru);
pane.add(chazhaonext);
pane.add(quxiao);
this.getContentPane().add(pane);
chazhaonext.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
search = shuru.getText();
String textst=writebroad.text.getText();
//高亮标记出查找到的所有内容
highlighter =writebroad.text.getHighlighter();
if(textst.contains(search))
{
int index=textst.indexOf(search);
while (true)
{
if(index!=-1)
{
try{
highlighter.addHighlight(index,index+search.length(), DefaultHighlighter.DefaultPainter);
}catch(BadLocationException e)
{
e.printStackTrace();
}
index=textst.indexOf(search,++index);
}
else
break;
}
}
int count=0;
//(循环+指定起始位置)
int t=0;
for(int i=0;i<textst.length();i++){
t=textst.indexOf(search,i);
if(t!=-1)
{
count++;//次数增加
i=t;//指定起始位置
Rectangle rec = null;
try {
rec = writebroad.text.modelToView(i);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
h=rec.y / rec.height + 1;
writebroad.text.select(t,t+ search.length());
findposition=findposition+(" 第 "+h+" 行, 第 "+(t+1)+" 列\n");
}
}
JOptionPane.showMessageDialog(null,findposition+(" \n 出现的次数共:"+count+"次"),"消息对话框",JOptionPane.PLAIN_MESSAGE);
writebroad.findflag=true;
dispose();
}
});
quxiao.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dispose();
highlighter.removeAllHighlights();
}
});
}
}