英文:
How do I colorise certain words in JTextPane?
问题
我有一个简单的HTML文本编辑器,我正在尝试将其转换为一个IDE。我已经完成了基本功能(比如添加文件浏览器和运行项目)。我面临的下一个障碍是添加主题。
我进行了谷歌搜索,并找到了这个问题,但它要求在更改文本颜色之前添加文本。
我想要的行为类似于JetBrains Webstorm(或IntelliJ IDE):
英文:
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):
答案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));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论