-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindJDialog.java
More file actions
213 lines (189 loc) · 6.11 KB
/
Copy pathfindJDialog.java
File metadata and controls
213 lines (189 loc) · 6.11 KB
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
package notepad;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
public class findJDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
private final ButtonGroup buttonGroup = new ButtonGroup();
private Highlighter highlighter;
private HighlightPainter painter_yellow = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow);
private HighlightPainter painter_orange = new DefaultHighlighter.DefaultHighlightPainter(Color.orange);
int n;
int position;
static boolean isopen = false;
/**
* Launch the application.
*/
// public static void main(String[] args) {
// try {
// findJDialog dialog = new findJDialog();
// dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// dialog.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/*
* 设置静态方法 判断是否打开
*/
public static boolean getisopen()
{
return isopen;
}
/**
* Create the dialog.
*/
public findJDialog(JTextArea textArea) {
setTitle("查找");
// 设置一直在前面
isopen = true;
setAlwaysOnTop(true);
setBounds(100, 100, 705, 339);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel label = new JLabel("查找内容");
label.setBounds(57, 63, 72, 18);
contentPanel.add(label);
textField = new JTextField();
textField.setBounds(168, 57, 384, 24);
contentPanel.add(textField);
textField.setColumns(10);
JCheckBox checkword = new JCheckBox("区分大小写");
checkword.setBounds(64, 168, 133, 27);
contentPanel.add(checkword);
JRadioButton up = new JRadioButton("向上");
buttonGroup.add(up);
up.setBounds(345, 168, 59, 27);
contentPanel.add(up);
JRadioButton down = new JRadioButton("向下");
down.setSelected(true);
buttonGroup.add(down);
down.setBounds(413, 168, 72, 27);
contentPanel.add(down);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s = textArea.getText();
String str = textField.getText();
highlighter = textArea.getHighlighter();
highlighter.removeAllHighlights();
/*
* 判断是否忽略大小写;
* 如果忽略大小写的话,把匹配和被匹配的字符全变成小写
* 否在不变
*/
if(checkword.isSelected() == false) {
s = s.toLowerCase();
str = str.toLowerCase();
}
/*
* 获取指针,找到下一个匹配字符,定位到此处并标橙色
* 具体做法是:判断是否存在匹配的字符 如果存在 就判断是否向上查找还是向上查找
* 其中我加上了循环查找
*/
position = textArea.getCaretPosition();
if(s.indexOf(str) == -1) {
setAlwaysOnTop(false);
JOptionPane.showMessageDialog(null, "没有找到匹配的字符", "查找", 0);
setAlwaysOnTop(true);
}else {
if(down.isSelected()) {
position = s.indexOf(str, position);
if(position == -1)position = s.indexOf(str);
System.out.println("index" + position);
textArea.setCaretPosition(position+str.length());
try {
highlighter.addHighlight(position, position+str.length(), painter_orange);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
position = s.lastIndexOf(str, position - str.length());
if(position == -1) {
position = s.lastIndexOf(str, s.length());
System.out.println("找不到的" + position);
}
System.out.println("last = " + position);
textArea.setCaretPosition(position);
try {
highlighter.addHighlight(position, position+str.length(), painter_orange);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 把其他匹配的字符标成黄色
*/
n = s.indexOf(str);
while(n != -1) {
try {
if(n != position)
{
highlighter.addHighlight(n, n+str.length(), painter_yellow);
}
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
n = s.indexOf(str, n+str.length());
}
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
isopen = false;
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
/*
* 检测是否关闭查找窗口,如果是把isopen标志 改为false
*/
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
isopen = false;
}
});
}
}