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

发布于 2024-10-19 20:26:33 字数 1719 浏览 1 评论 0原文

我正在尝试确定文本区域中一行文本的边界。我知道我可以确定偏移量的界限,这意味着我需要知道行的开头和结尾的偏移量。

所以我想尝试使用 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);
    }
}

它突出显示的是整个文本段落,但我只想要包含我正在搜索的术语的行。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-10-26 20:26:33
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));
    }
}
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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文