-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtihuan.java
278 lines (198 loc) · 6.52 KB
/
tihuan.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
//实现菜单里 替换功能 对话框
public class tihuan extends JDialog implements ActionListener {
JLabel chazhao,tihuanwei;
JTextField shuru,shuru2;
JButton chazhaonext,tihuan,quxiao,quanbu;
JCheckBox qufen;
JRadioButton up,down;
public tihuan(JFrame own,String title,boolean bo)
{
super(own,title,bo);
chazhao=new JLabel("查找内容(N):");
tihuanwei=new JLabel("替换为:");
qufen=new JCheckBox("区分大小写");
shuru=new JTextField();
shuru2=new JTextField();
shuru2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent eee) {
tihuan.setEnabled(true);
quanbu.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));
}
});
shuru2.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent ee){
shuru2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
}
});
shuru2.addMouseListener(new MouseAdapter()
{
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
shuru2.setBorder(javax.swing.BorderFactory.createLineBorder(Color.blue));
}
});
chazhaonext=new JButton("查找下一个");
chazhaonext.addActionListener(this);
tihuan=new JButton("替换");
tihuan.setEnabled(false);
tihuan.addActionListener(this);
quanbu=new JButton("全部替换");
quanbu.setEnabled(false);
quanbu.addActionListener(this);
quxiao=new JButton("取消");
quxiao.addActionListener(this);
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(485,185);
this.setLocationRelativeTo(null);
JPanel pane=new JPanel();
pane.setLayout(null);
chazhao.setBounds(10, 10, 80, 30);
shuru.setBounds(83,15,200, 22);
tihuanwei.setBounds(10,40,80,30);
shuru2.setBounds(83,45, 200, 22);
chazhaonext.setBounds(300,15,120,22);
tihuan.setBounds(300,45, 120,22);
quanbu.setBounds(300,75,120,22);
quxiao.setBounds(300,105,120,22);
qufen.setBounds(10,100, 110, 30);
fanxiang.setBounds(125, 80, 160, 60);
pane.add(chazhao);
pane.add(shuru);
pane.add(tihuanwei);
pane.add(shuru2);
pane.add(chazhaonext);
pane.add(tihuan);
pane.add(quanbu);
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);
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String search = shuru.getText();
String textst=writebroad.text.getText();
String replace=shuru2.getText();
String newText;
if(arg0.getSource()==quanbu)
{
newText=textst.replace(search, replace);
writebroad.text.setText(newText);
JOptionPane.showMessageDialog(null, "系统已经为您全部替换");
setVisible(false);
}
else if(arg0.getSource()==tihuan)
{
newText=textst.replaceFirst(search, replace);
writebroad.text.setText(newText);
JOptionPane.showMessageDialog(null, "系统已经为您替换了一个符合的字符串");
}
else if(arg0.getSource()==quxiao)
{
dispose();
}
}
}