gpt4 book ai didi

java - 在文本编辑器中查找上一个单词

转载 作者:行者123 更新时间:2023-11-30 06:14:08 26 4
gpt4 key购买 nike

你好,下面的代码是一个带有一些实用程序的文本编辑器,但我无法找出为什么我的 doPre() 方法找不到我搜索的前一个词,就像 doNext() 方法找到我搜索的下一个词一样。有什么问题所以它会正常工作?提前致谢!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyEditor extends JFrame {

public MyEditor() {
super("MyEditor");
initComponents();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void initComponents() {
p = new JPanel(new BorderLayout());
ta = new JTextArea(10, 50);

sc = new JScrollPane(ta);
p.add(sc);

tb = new JToolBar();
tb.setFloatable(false);
newButton = new JButton("new");
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doNew();
}
});
newButton.setToolTipText("New file...");
newButton.setIcon(new ImageIcon(getClass().getResource("/images/New16.png")));
openButton = new JButton("open");
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doOpen();
}
});
openButton.setToolTipText("Open a file...");
openButton.setIcon(new ImageIcon(getClass().getResource("/images/Open16.gif")));

saveButton = new JButton("save");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doSave();
}
});
saveButton.setToolTipText("Save a file...");
saveButton.setIcon(new ImageIcon(getClass().getResource("/images/Save16.gif")));
findButton = new JButton("find");
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doFind();
}
});
findButton.setToolTipText("Find...");
findButton.setIcon(new ImageIcon(getClass().getResource("/images/Search16.png")));
nextButton = new JButton("next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doNext();
}
});
nextButton.setToolTipText("Next...");
nextButton.setEnabled(false);
previousButton = new JButton("pre");
previousButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doPre();
}
});
previousButton.setToolTipText("P...");
previousButton.setEnabled(true);
aboutButton = new JButton("about");
aboutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doAbout();
}
});
aboutButton.setToolTipText("About...");
aboutButton.setIcon(new ImageIcon(getClass().getResource("/images/Info16.png")));
tb.add(newButton);
tb.add(openButton);
tb.add(saveButton);
tb.add(findButton);
tb.add(nextButton);
tb.add(previousButton);
tb.add(aboutButton);
p.add(tb, BorderLayout.NORTH);
add(p);
fc = new JFileChooser();
pack();
}

private void doOpen() {
fc.showOpenDialog(this);
theFile = fc.getSelectedFile();
if (theFile == null) {
return;
}

ta.setText(null);

FileReader myFile = null;
BufferedReader buff = null;

try {
myFile = new FileReader(theFile);
buff = new BufferedReader(myFile);

while (true) {
String line = buff.readLine();
if (line == null) { //EOF
break;
}
ta.append(line + "\n");
}
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (buff != null) {
buff.close();
}
if (myFile != null) {
myFile.close();
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
}

private void doSave() {
fc.showSaveDialog(this);
theFile = fc.getSelectedFile();
if (theFile == null) {
return;
}

FileWriter myFile = null;
BufferedWriter buff = null;

try {
myFile = new FileWriter(theFile);
buff = new BufferedWriter(myFile);
buff.write(ta.getText());

System.out.println("File writing is complete");
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (buff != null) {
buff.flush();
buff.close();
}
if (myFile != null) {
myFile.close();
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}

}

private void doNew() {
ta.setText(null);
}

private void doFind() {
findString = JOptionPane.showInputDialog(this, "Find What", "Find", JOptionPane.INFORMATION_MESSAGE);
ta.requestFocusInWindow();
if (findString != null && findString.length() > 0) {
String txt = ta.getText();
int findLength = findString.length();
findLocation = txt.indexOf(findString);
if (findLocation > -1) {
ta.setCaretPosition(findLocation);
ta.moveCaretPosition(findLocation + findLength);
ta.getCaret().setSelectionVisible(true);
nextButton.setEnabled(true);
}
}
}

private void doNext() {
String txt = ta.getText();
int findLength = findString.length();
if (findLocation > -1) {
findLocation = txt.indexOf(findString, findLocation + 1);
if (findLocation > -1) {
ta.setCaretPosition(findLocation);
ta.moveCaretPosition(findLocation + findLength);
ta.getCaret().setSelectionVisible(true);
} else {
nextButton.setEnabled(false);
}
}

}
private void doPre() {
String txt = ta.getText();
int findLength = findString.length();
if (findLocation > -1) {
findLocation = txt.indexOf(findString, (findLocation-1) + 1);
if (findLocation > -1) {
ta.setCaretPosition(findLocation);
ta.moveCaretPosition((findLocation-(findLocation+1)) + findLength +1);
ta.getCaret().setSelectionVisible(true);
} else {
nextButton.setEnabled(false);
}
}
private void doAbout() {
JOptionPane.showMessageDialog(this, "TEI Java Editor\nSummer Semester 2015", "About", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyEditor().setVisible(true);
}
});
}

private JPanel p;
private JTextArea ta;
private JScrollPane sc;
private JToolBar tb;
private JButton newButton, openButton, saveButton, findButton, aboutButton, nextButton, previousButton;
private JFileChooser fc;
private File theFile;
private String findString;
private int findLocation;
}

到目前为止,我设法让它转到前一个词,但我不知道如何让它位于顶部。例如,如果我们在文本编辑器中得到 4 次相同的单词:

查找位置
查找位置
查找位置
查找位置

我当前的状态标记了第 3 个单词,它将跳转并标记第 2 个单词,如果我再次按下上一个按钮,它不会跳转并标记第 1 个单词。有什么想法吗?

private void doPre() {
String txt = ta.getText();
int findLength = findString.length();
if (findLocation > -1) {
findLocation = txt.lastIndexOf(findString, findLocation + 1);
if (findLocation > -1) {
ta.setCaretPosition(findLocation);
ta.moveCaretPosition(findLocation - findLength - 1);
ta.getCaret().setSelectionVisible(true);
} else {
previousButton.setEnabled(false);
}
}

最佳答案

我注意到在你的 doPre 方法中你有这个

findLocation = txt.indexOf(findString, (findLocation-1) + 1);

如果减一然后再加一,findLocation 似乎会完全相同。试着去掉 +1;

findLocation = txt.indexOf(findString, findLocation - 1);

这行也是一样的:

ta.moveCaretPosition((findLocation-(findLocation+1)) + findLength +1);

在这里,findLocation - findLocation + 1 只会给你 1,这是多余的。

关于java - 在文本编辑器中查找上一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com