当前位置: 技术问答>java相关
怎样实现“查找”功能。(一定给分!)
来源: 互联网 发布时间:2015-11-14
本文导语: 初学JAVA,用JAVA作了一个文本编辑器,不知道怎样才能实现象Windows记事本中的查找功能。 例如:文本区中有一篇文章,在查找框中输入某关键字,点击查找按扭后找出关键字在文本中的位置。(用高亮度或其他颜色显...
初学JAVA,用JAVA作了一个文本编辑器,不知道怎样才能实现象Windows记事本中的查找功能。
例如:文本区中有一篇文章,在查找框中输入某关键字,点击查找按扭后找出关键字在文本中的位置。(用高亮度或其他颜色显示出来)。
请各位指点,或者哪里有类似源代码请告知。谢了!一定给分!
例如:文本区中有一篇文章,在查找框中输入某关键字,点击查找按扭后找出关键字在文本中的位置。(用高亮度或其他颜色显示出来)。
请各位指点,或者哪里有类似源代码请告知。谢了!一定给分!
|
找段网上的例子,供你参考:
else if(ae.getSource() == menuEditFind) {
JDialog ds = new JDialog(this, "Find", true);
ds.getContentPane().setLayout(new FlowLayout());
ds.setResizable(false);
final JLabel dsMessage1 = new JLabel(" Found counts: ");
final JLabel dsMessage2 = new JLabel(" 0");
final Checkbox dsLoop = new Checkbox("Loop ");
dsLoop.setState(findingLoop);
final Checkbox dsMatchCase = new Checkbox("Match Case ");
final TextField tfs = new TextField(15);
ds.getContentPane().add(tfs);
tfs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfs.getText();
str4 = str3.toLowerCase();
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if(a > -1) {
ta.setCaretPosition(a);
b = tfs.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bs = new Button(" Find ");
//same as tfs.addActionListener(new ActionListener() {
bs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfs.getText();
str4 = str3.toLowerCase();
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if(a > -1) {
ta.setCaretPosition(a);
b = tfs.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bsc = new Button("Cancel");
bsc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
foundCount = 0;
}
});
ds.getContentPane().add(bs);
ds.getContentPane().add(bsc);
ds.getContentPane().add(dsLoop);
ds.getContentPane().add(dsMatchCase);
ds.getContentPane().add(dsMessage1);
ds.getContentPane().add(dsMessage2);
ds.setLocation(120, 120);
ds.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
ds.setSize(260,110);
ds.setVisible(true);
}
else if(ae.getSource() == menuEditFind) {
JDialog ds = new JDialog(this, "Find", true);
ds.getContentPane().setLayout(new FlowLayout());
ds.setResizable(false);
final JLabel dsMessage1 = new JLabel(" Found counts: ");
final JLabel dsMessage2 = new JLabel(" 0");
final Checkbox dsLoop = new Checkbox("Loop ");
dsLoop.setState(findingLoop);
final Checkbox dsMatchCase = new Checkbox("Match Case ");
final TextField tfs = new TextField(15);
ds.getContentPane().add(tfs);
tfs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfs.getText();
str4 = str3.toLowerCase();
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if(a > -1) {
ta.setCaretPosition(a);
b = tfs.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bs = new Button(" Find ");
//same as tfs.addActionListener(new ActionListener() {
bs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfs.getText();
str4 = str3.toLowerCase();
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if(a > -1) {
ta.setCaretPosition(a);
b = tfs.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bsc = new Button("Cancel");
bsc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
foundCount = 0;
}
});
ds.getContentPane().add(bs);
ds.getContentPane().add(bsc);
ds.getContentPane().add(dsLoop);
ds.getContentPane().add(dsMatchCase);
ds.getContentPane().add(dsMessage1);
ds.getContentPane().add(dsMessage2);
ds.setLocation(120, 120);
ds.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
ds.setSize(260,110);
ds.setVisible(true);
}
|
利用java string 的 indexOf 方法,等于-1是不存在了!
|
具体不大清楚,好象先建立一个模式,当然模式中包含要匹配的字符串,之后调用
该模式的match方法,该方法有一个参数就是你的计事本中的字符串。
该模式的match方法,该方法有一个参数就是你的计事本中的字符串。
|
gz