如何在JTextPane中着色特定单词?

huangapple go评论66阅读模式
英文:

How do I colorise certain words in JTextPane?

问题

我有一个简单的HTML文本编辑器,我正在尝试将其转换为一个IDE。我已经完成了基本功能(比如添加文件浏览器和运行项目)。我面临的下一个障碍是添加主题。

我进行了谷歌搜索,并找到了这个问题,但它要求在更改文本颜色之前添加文本。

我想要的行为类似于JetBrains Webstorm(或IntelliJ IDE):
如何在JTextPane中着色特定单词?

目前的界面如下所示:
如何在JTextPane中着色特定单词?

英文:

I have a simple HTML text Editor which I'm trying to convert into an IDE. I'm finished with the basics (Like adding a file browser and running the project). The next obstacle I'm facing is adding themes.

I googled and found this question but it requires adding the text before changing its color.
What behaviour I want is similar to JetBrains Webstorm (Or IntelliJ IDE):
如何在JTextPane中着色特定单词?

This is what it looks like as of now:
如何在JTextPane中着色特定单词?

答案1

得分: 1

@MadProgrammer提到的,你可以使用RSyntaxTextArea

从它的README中:

RSyntaxTextArea是一个可自定义的、支持语法高亮的Java Swing应用程序文本组件。它默认支持50多种编程语言的语法高亮、代码折叠、搜索和替换,并具有用于代码完成和拼写检查的附加库。可以通过工具如JFlex添加额外语言的语法高亮支持。

示例用法:-

import javax.swing.*;
import java.awt.BorderLayout;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class TextEditorDemo extends JFrame {

    public TextEditorDemo() {

        JPanel cp = new JPanel(new BorderLayout());

        RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
        textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        textArea.setCodeFoldingEnabled(true);
        RTextScrollPane sp = new RTextScrollPane(textArea);
        cp.add(sp);

        setContentPane(cp);
        setTitle("Text Editor Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);

    }

    public static void main(String[] args) {
        // 在EDT上启动所有Swing应用程序。
        SwingUtilities.invokeLater(() -> new TextEditorDemo().setVisible(true));
    }

}
英文:

Like @MadProgrammer mentioned, you can use RSyntaxTextArea.

From its README:

> RSyntaxTextArea is a customizable, syntax highlighting text component
> for Java Swing applications. Out of the box, it supports syntax
> highlighting for 50+ programming languages, code folding, search and
> replace, and has add-on libraries for code completion and spell
> checking. Syntax highlighting for additional languages can be added
> via tools such as JFlex.

Example Usage:-

import javax.swing.*;
import java.awt.BorderLayout;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class TextEditorDemo extends JFrame {

    public TextEditorDemo() {

        JPanel cp = new JPanel(new BorderLayout());

        RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
        textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        textArea.setCodeFoldingEnabled(true);
        RTextScrollPane sp = new RTextScrollPane(textArea);
        cp.add(sp);

        setContentPane(cp);
        setTitle("Text Editor Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);

    }

    public static void main(String[] args) {
        // Start all Swing applications on the EDT.
        SwingUtilities.invokeLater(() -> new TextEditorDemo().setVisible(true));
    }

}

huangapple
  • 本文由 发表于 2023年2月18日 15:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定