简体   繁体   English

换行时确定JTextArea行的文本偏移量

[英]Determining text offset of a JTextArea line when wrapping

I am trying to determine the bounds of a line of text in my text area. 我正在尝试确定文本区域中一行文本的边界。 I know that I can determine the bounds for an offset, which means I need to know the offset of the start and end of the line. 我知道我可以确定偏移量的范围,这意味着我需要知道该行起点和终点的偏移量。

So I thought I would try using the getLine* methods, but it didn't give the result I expected. 所以我想我会尝试使用getLine*方法,但是没有得到我期望的结果。

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;

public class TextAreaLineBoundsTest
{
    public static void main(String[] args) throws Exception
    {
        String string = "Lorem ipsum eum putant gubergren evertitur in, no assueverit vituperatoribus eum. Ea cibo offendit vim, est et vivendum qualisque prodesset. Vis doctus expetenda contentiones an, no ius mazim epicuri expetendis, saperet salutandi forensibus ne usu. Ex fugit alterum usu. His ignota cotidieque in, augue erroribus eam no.";
        JTextArea textArea = new JTextArea(string);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        String term = "qualisque";
        int termOffset = string.indexOf(term);

        int termLine = textArea.getLineOfOffset(termOffset);
        int termLineStartOffset = textArea.getLineStartOffset(termLine);
        int termLineEndOffset = textArea.getLineEndOffset(termLine);

        textArea.getHighlighter().addHighlight(termLineStartOffset, termLineEndOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));

        JScrollPane textAreaScroll = new JScrollPane(textArea);

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(textAreaScroll, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

What it has highlighted is the entire paragraph of text, but I just want the line which contains the term I'm searching for. 它突出显示的是文本的整个段落,但我只想要包含要搜索的术语的行。

import java.awt.*;
import javax.swing.*;
import javax.swing.text.DefaultHighlighter;

public class TextAreaLine
{
    public static void main(String[] args) throws Exception
    {
        String string = "Lorem ipsum eum putant gubergren evertitur in, no assueverit vituperatoribus eum. Ea cibo offendit vim, est et vivendum qualisque prodesset. Vis doctus expetenda contentiones an, no ius mazim epicuri expetendis, saperet salutandi forensibus ne usu. Ex fugit alterum usu. His ignota cotidieque in, augue erroribus eam no.";
        JTextArea textArea = new JTextArea(string);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane textAreaScroll = new JScrollPane(textArea);

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(textAreaScroll, BorderLayout.CENTER);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        String term = "qualisque";
        int termOffset = string.indexOf(term);
        Rectangle view = textArea.modelToView(termOffset);
        int startOffset = textArea.viewToModel(new Point(0, view.y));
        int endOffset = textArea.viewToModel(new Point(textArea.getSize().width, view.y) );
        textArea.getHighlighter().addHighlight(startOffset, endOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM