Java – 滚动到JTextArea内的特定文本

前端之家收集整理的这篇文章主要介绍了Java – 滚动到JTextArea内的特定文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我正在写的当前程序中实现一个功能,我想学习如何向下滚动到JTextArea中的特定文本.例如,让我说我有以下:
JTextArea area = new JTextArea(someReallyLongString);

someReallyLongString将表示一段或一段非常大的文本(垂直滚动条可以在其中显示).所以我想要做的是向下滚动到该文本区域内的特定文本.例如,让someReallyLongString在滚动条的中间附近包含单词“the”(意思是这个词不可见),我将如何向下滚动到那个特定的文本?

谢谢,任何帮助将会非常感激.

解决方法

这是一个非常基本的例子.这样就基本上走过文档来查找单词在文档中的位置,并确保文本被移动到可视区域.

它也突出了比赛

public class MoveToText {

    public static void main(String[] args) {
        new MoveToText();
    }

    public MoveToText() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FindTextPane());
                frame.setSize(400,400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FindTextPane extends JPanel {

        private JTextField findField;
        private JButton findButton;
        private JTextArea textArea;
        private int pos = 0;

        public FindTextPane() {
            setLayout(new BorderLayout());
            findButton = new JButton("Next");
            findField = new JTextField("Java",10);
            textArea = new JTextArea();
            textArea.setWrapStyleWord(true);
            textArea.setLineWrap(true);

            Reader reader = null;
            try {
                reader = new FileReader(new File("Java.txt"));
                textArea.read(reader,null);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (Exception e) {
                }
            }

            JPanel header = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            header.add(findField,gbc);
            gbc.gridx++;
            header.add(findButton,gbc);

            add(header,BorderLayout.NORTH);
            add(new JScrollPane(textArea));

            findButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Get the text to find...convert it to lower case for eaiser comparision
                    String find = findField.getText().toLowerCase();
                    // Focus the text area,otherwise the highlighting won't show up
                    textArea.requestFocusInWindow();
                    // Make sure we have a valid search term
                    if (find != null && find.length() > 0) {
                        Document document = textArea.getDocument();
                        int findLength = find.length();
                        try {
                            boolean found = false;
                            // Rest the search position if we're at the end of the document
                            if (pos + findLength > document.getLength()) {
                                pos = 0;
                            }
                            // While we haven't reached the end...
                            // "<=" Correction
                            while (pos + findLength <= document.getLength()) {
                                // Extract the text from teh docuemnt
                                String match = document.getText(pos,findLength).toLowerCase();
                                // Check to see if it matches or request
                                if (match.equals(find)) {
                                    found = true;
                                    break;
                                }
                                pos++;
                            }

                            // Did we find something...
                            if (found) {
                                // Get the rectangle of the where the text would be visible...
                                Rectangle viewRect = textArea.modelToView(pos);
                                // Scroll to make the rectangle visible
                                textArea.scrollRectToVisible(viewRect);
                                // Highlight the text
                                textArea.setCaretPosition(pos + findLength);
                                textArea.moveCaretPosition(pos);
                                // Move the search position beyond the current match
                                pos += findLength;
                            }

                        } catch (Exception exp) {
                            exp.printStackTrace();
                        }

                    }
                }
            });

        }
    }
}

猜你在找的Java相关文章