在 java swing jtext 组件中突出显示更改文本

发布于 2024-11-15 23:26:09 字数 2783 浏览 3 评论 0原文

我试图在 JEditorPane 中突出显示一些代码,如下所示:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //store all the text at once
            pane.setText(text);

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

这将一次突出显示一个字符,并且所有字符将保持突出显示。现在,我想要相同的行为(所有字符保持突出显示),但我想更改突出显示之间的文本,如下所示:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //place a new string in the pane
                pane.setText(pane.getText() + text.charAt(i));

                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

注意窗格中的文本正在更改,然后我应用新的突出显示。旧的亮点消失了——我希望它们留下来。我的假设是每次 setText() 时突出显示都会消失。那么,有没有办法在改变文本的同时,保持文本组件中的高亮显示呢?

I am trying to highlight some code in a JEditorPane like this:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //store all the text at once
            pane.setText(text);

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

This will highlight the characters one at a time and all the characters will remain highlighted. Now, I'd like the same behavior (all the characters remain highlighted) but I'd like to change the text in between highlights, like this:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class Driver
{
    public static void main(String[] args)
    {
        try
        {
            //create a simple frame with an editor pane
            JFrame frame = new JFrame("Highlight Test");
            JEditorPane pane = new JEditorPane();
            frame.getContentPane().add(pane);
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            //string to put in the pane
            String text = "1234567890";

            //grab the highlighter for the pane
            Highlighter highlighter = pane.getHighlighter();

            //go through all the characters
            for(int i = 0; i < text.length(); i++)
            {
                //place a new string in the pane
                pane.setText(pane.getText() + text.charAt(i));

                //highlight the latest character
                highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

                //sleep for a quarter second
                Thread.sleep(250);
            }
        }catch(Exception ex){}
    }

}

Notice the text in the pane is changing and then I'm applying a new highlight. The old highlights go away- I'd like them to stay. My assumption is the highlights go away each time you setText(). So, is there any way to keep the highlights in the text component while changing the text?

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

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

发布评论

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

评论(1

傻比既视感 2024-11-22 23:26:09

我没有尝试以下代码,但我建议只是尝试突出显示最新字符和以前的字符,如下所示:

        //go through all the characters
        for(int i = 0; i < text.length(); i++)
        {
            //place a new string in the pane
            pane.setText(pane.getText() + text.charAt(i));
          //highlight the previous characters
          if (i > 0) {
           for ( int j=i-1; j >= 0; j--)
             highlighter.addHighlight(j, j+1 , DefaultHighlighter.DefaultPainter);
           }
            //highlight the latest character
            highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

            //sleep for a quarter second
           // Thread.sleep(250);
        }
    }catch(Exception ex){}

i didn't tried the following code but what i suggest is just try to highlight both latest character and previous ones too like this:

        //go through all the characters
        for(int i = 0; i < text.length(); i++)
        {
            //place a new string in the pane
            pane.setText(pane.getText() + text.charAt(i));
          //highlight the previous characters
          if (i > 0) {
           for ( int j=i-1; j >= 0; j--)
             highlighter.addHighlight(j, j+1 , DefaultHighlighter.DefaultPainter);
           }
            //highlight the latest character
            highlighter.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);

            //sleep for a quarter second
           // Thread.sleep(250);
        }
    }catch(Exception ex){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文