FindPrevious在文本编辑器中的单词

时间:2015-06-12 19:38:02

标签: java swing

您好以下代码是一个带有一些实用程序的文本编辑器,但是我无法找出为什么我的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倍,如:

findLocation
findLocation
findLocation
findLocation

并且我当前状态标记了它将跳跃并标记第二个单词的第三个单词,如果我再次按下前一个按钮它就不会跳跃并标记第一个单词。任何想法?

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);
        }
    }

2 个答案:

答案 0 :(得分:4)

我注意到你的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,这是多余的。

答案 1 :(得分:1)

我不知道它是什么意思“无法正常工作” 只有我看到的是:

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

简化:

( findLocation - ( findLocation + 1 ) ) + findLength +1

为:

( findLocation - findLocation - 1) + findLength + 1 =
( -1 ) + findLength + 1 =
findLength

所以这意味着:

ta.moveCaretPosition(findLength);
相关问题